Android Fragments Activity com.example.BasicActivity泄露了最初添加到此处的窗口DecorView@ec0c7fa[BasicActivity]

li9yvcax  于 2023-10-19  发布在  Android
关注(0)|答案(1)|浏览(100)

我有一些问题,但我不知道是什么原因导致这个问题。一旦我点击设置为壁纸按钮对话框自动消失,甚至有时设置壁纸需要很长时间。我尝试设置包括其他解决方案,如包括方法progressDialog!=null但没有任何工作。
这是错误

android.view.WindowLeaked: Activity com.example.BasicActivity has leaked window DecorView@bc3f725[BasicActivity] that was originally added here
       at android.view.ViewRootImpl.<init>(ViewRootImpl.java:1009)
       at android.view.ViewRootImpl.<init>(ViewRootImpl.java:997)
       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:397)
       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:150)
       at android.app.Dialog.show(Dialog.java:352)
       at androidx.appcompat.app.AlertDialog$Builder.show(AlertDialog.java:1009)
       at com.exmaple.SlideshowDialogFragment$4$1.onBitmapLoaded(SlideshowDialogFragment.java:678)
       at com.squareup.picasso.TargetAction.complete(TargetAction.java:36)
       at com.squareup.picasso.Picasso.deliverAction(Picasso.java:576)
       at com.squareup.picasso.Picasso.complete(Picasso.java:528)
       at com.squareup.picasso.Picasso$1.handleMessage(Picasso.java:122)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loopOnce(Looper.java:205)
       at android.os.Looper.loop(Looper.java:294)
       at android.app.ActivityThread.main(ActivityThread.java:8177)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

基本活动

public class BasicActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_basic);
    ...
    Fragment frag = new Sample_Gallery();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.basic_fragment,frag).commit();

Sample_Gallery.Java

FragmentTransaction ft = null;
                if (getFragmentManager() != null) {
                    ft = getFragmentManager().beginTransaction();
                    SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
                    newFragment.setArguments(bundle);
                    newFragment.show(ft, "slideshow");
                }

滑动

set_as_wal.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getActivity() != null) {
                progressDialog = ProgressDialog.show(getActivity(), "Please wait...", "Set as your wallpaper...", false, true);

                Picasso.get().load(setImgLink).into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity());
                        try {
                            wallpaperManager.setBitmap(bitmap);
                            
                            progressDialog.dismiss();
                            ct_popup.setText("Wallpaper was set");
                            final AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
                            builder1.setView(subView);
                            builder1.setPositiveButton("Close", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                   
                                    ((ViewGroup) subView.getParent()).removeView(subView);
                                }
                            });
                            builder1.setIcon(R.mipmap.ic_launcher);
                            builder1.setCancelable(false);
                            builder1.create();
                            builder1.show();
                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.e(TAG, "IOException->" + e.getMessage());
                        }
                    }

                    @Override
                    public void onBitmapFailed(Exception e, Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
            }
        }
    });
bz4sfanl

bz4sfanl1#

我猜这个问题是因为设置壁纸时ProgressDialog会消失。您可以在调用dismiss()之前检查progressDialog是否仍然显示。例如:

try {
wallpaperManager.setBitmap(bitmap);
if (progressDialog != null && progressDialog.isShowing()) {
    progressDialog.dismiss();
}
//...

这将确保ProgressDialog仅在仍显示时才被关闭。

相关问题