Android: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity

前端 未结 6 1410
-上瘾入骨i
-上瘾入骨i 2021-01-30 16:06

I\'d like to ask for some help: In my app, I have only one activity, a PreferenceActivity (don\'t need other, it\'s just a simple background-sync app, so the

6条回答
  •  情话喂你
    2021-01-30 16:47

    I had a very similar issue (which landed me here) and found a very simple fix for it. While my code is different, it should be easy to adapt. Here is my fix:

    public void showBox() {
        mActive = true;
        if (! ((Activity) mContext).isFinishing()) {
            mDialogBox.show();
        }
    } 
    

    So, in the sample code in the question, the fix would have been (at a guess):

    @Override
    public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
        if (key.equals("checkTest")) {
            if (! this.isFinishing()) {
                showDialog(1);
            }
        }
        if (key.equals("cancel")) {
            dismissDialog(1);
        }
    }// onSPC
    

提交回复
热议问题