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

问题描述 投票:0回答:1

我有一些问题,但我不知道是什么导致了这个问题。一旦我单击“设置为壁纸”按钮,对话框就会自动消失,即使有时设置壁纸需要很长时间。我尝试设置包含其他解决方案,例如包含 ondrstroy 方法 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) {

                    }
                });
            }
        }
    });
android android-fragments dialog android-bitmap android-wallpaper
1个回答
0
投票

我猜问题是因为设置壁纸时 ProgressDialog 消失了。您可以在调用 Dismiss() 之前添加检查

progressDialog
是否仍然显示。 比如:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.