Android无法添加窗口

时间:2011-06-01 23:48:48

标签: android alertdialog

我有一个包含复选框的PreferenceActivity,并且工作得很好。我决定添加一个AlertDialog,它首先提示用户确保他们希望选中该复选框。十分之九的一切都运行正常但有时特别是当我尝试在加载ActivityPreference时检查一个复选框时,我在尝试显示对话框时遇到以下错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@45a646d8 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:468)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:239)
at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
at com.beargraves.hitechmedia.AppPreferenceActivity.onSharedPreferenceChanged(AppPreferenceActivity.java:385)
at android.app.ApplicationContext$SharedPreferencesImpl$EditorImpl.commit(ApplicationContext.java:2861)
at android.preference.Preference.tryCommit(Preference.java:1199)
at android.preference.Preference.persistBoolean(Preference.java:1404)
at android.preference.CheckBoxPreference.setChecked(CheckBoxPreference.java:154)
at android.preference.CheckBoxPreference.onClick(CheckBoxPreference.java:143)
at android.preference.Preference.performClick(Preference.java:811)
at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:190)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3606)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1751)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)

I saw this question但答案似乎并不相关。这是我的代码

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
    final String k = key;
    if (findPreference(key) instanceof CheckBoxPreference) {
        final CheckBoxPreference chkPref = (CheckBoxPreference) findPreference(key);

        boolean result = sharedPreferences.getBoolean(key,false);
            if(result){

                new AlertDialog.Builder(AppPreferenceActivity.this)

                .setTitle("Confirm Ation")

                .setMessage("Do you wish to: " + chkPref.getTitle())

                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        pDialog.setMessage("Syncing");
                        pDialog.show();
                        new SyncTask().execute(k);

                    }
                })

                .setNegativeButton("No",

                        new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        chkPref.setChecked(false);

                    }

                }).show();

            }

    }

}

非常感谢任何帮助。

0 个答案:

没有答案