AsyncTask完成后关闭对话框时出现WindowManager $ BadTokenException

时间:2018-07-31 15:45:12

标签: android android-asynctask dialog

我一直在我的C​​rashlytics日志中看到偶尔的崩溃,但是我无法复制自己。我到处搜索,并认为我已经遵循了处处显示的相同准则来处理如何围绕AsyncTask创建和关闭对话框,即从onPreExecute()创建和显示对话框,并在onPostExecute()中关闭它们。我在这里想念什么吗?

这是该课程的完整代码:

public class MyActivity extends BaseActivity implements AsyncResponse {


    private AlertDialog mAlert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        populateData();
    }

    public void processFinish(boolean result){
        if(result) {
            //setup and start new intent
            finish();
        }
        else{
            AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
            //setup alert
            mAlert = builder.create();
            mAlert.show();
        }
    }

    private void populateData() {
        boolean hasConnection = Utility.isNetworkAvailable(this);
        if(!hasConnection){
            showConnectDialog();
        }
        MyTask myTask = new MyTask(this);
        myTask.delegate = this;
        myTask.execute();
    }

    @Override
    protected void onPause(){
        super.onPause();
        if ( mAlert!=null && mAlert.isShowing() ){
            mAlert.cancel();
        }
    }

    private static ProgressDialog mProgressDialog;
    private static void showProgressDialog(Context context) {

        if (mProgressDialog == null) {
            //setup dialog
        }

        if (! ((Activity) context).isFinishing()) {
            mProgressDialog.show();
        }
    }

    private static void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    protected void onDestroy() {
        dismissProgressDialog();
        super.onDestroy();
    }

    private static class MyTask extends AsyncTask<Void, Void, Boolean> {

        final WeakReference<BaseActivity> activityRef;

        private AsyncResponse delegate=null;

        private PowerManager.WakeLock mWakeLock;


        private MyTask(BaseActivity activity) {
            activityRef = new WeakReference<>(activity);
        }

        @Override
        protected void onPreExecute() {
            // take CPU lock to prevent CPU from going off if the user
            // presses the power button during download
            Context context = activityRef.get();
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
            mWakeLock.acquire(90000); //1.5 minute timeout

            int currentOrientation = context.getResources().getConfiguration().orientation;
            if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
                ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else {
                ((Activity) context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
            showProgressDialog(context);
        }

        @Override
        protected void onPostExecute(Boolean result) {
            mWakeLock.release();
            dismissProgressDialog();
            BaseActivity activity = activityRef.get();
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
            delegate.processFinish(result);
        }



        @Override
        protected Boolean doInBackground(Void... params) {
            DataRetriever retriever = new DataRetriever(activityRef.get());
            return retriever.getData();
        }
    }

}

异常日志显示:     由android.view.WindowManager $ BadTokenException引起     无法添加窗口-令牌android.os.BinderProxy@a60eb7a0无效;您的活动正在进行吗?

android.view.ViewRootImpl.setView (ViewRootImpl.java:559)
android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:269)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:69)
android.app.Dialog.show (Dialog.java:281)
com.xxx.MyActivity.showProgressDialog (MyActivity.java:1100)
com.xxx.MyActivity$MyTask.onPreExecute (MyActivity.java:95)
android.os.AsyncTask.executeOnExecutor (AsyncTask.java:586)
android.os.AsyncTask.execute (AsyncTask.java:534)
com.xxx.MyActivity.populateLanguages (LanguagePopulateActivity.java:32)
com.xxx.MyActivity.onCreate (LanguagePopulateActivity.java:9)

谢谢!

0 个答案:

没有答案