1

I have a PreferenceActivity containing checkboxes amongst other things and worked perfectly. I decided to add an AlertDialog which first prompts the user to ensure they wish to select the check box. Nine times out of 10 everything works fine but on occasion specifically when I try to check a check box as soon as the ActivityPreference has loaded I get the following error when trying to show the dialog:

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 but the answers didn't appear relevant. Here is my code

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();

            }

    }

}

Any help would be much appreciated.

Community
  • 1
  • 1
Bear
  • 1,541
  • 3
  • 20
  • 32

0 Answers0