Hi lama,

I once had this android.view.WindowManager$BadTokenException when i
was trying to load the dialog too early.
It was resolved by delaying the showdialog call.
U can try it by using Handler().postDelayed()

Thanks,
vivek


On Sep 14, 10:38 pm, lama <viny...@gmail.com> wrote:
> Hello Masters!
>
> 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 PrefsActivity is the Main/Launcher). After the user
> setup preferences, checks a checkBoxPreference, and that starts (or
> stops) a service. At starting, a dialog shows. But here is the
> problem: if the user press back (leave the activity), start it again,
> and than tries to check the checkBoxPref., the prefsactivity crashes.
> Dialog doesn't shows. I have no idea why, and how to fix it.
>
> This code like this below, (this is exsactly same with that part, what
> gives me the problem):
>
> PrefsActivity.java:
>
> package is.it.works;
>
>    // imports .....
>
> public class PrefsActivity extends PreferenceActivity implements
> OnSharedPreferenceChangeListener { SharedPreferences prefs;
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
>     super.onCreate(savedInstanceState);
>     addPreferencesFromResource(R.xml.prefs);
>     prefs = PreferenceManager.getDefaultSharedPreferences(this);
>     prefs.registerOnSharedPreferenceChangeListener(this);
>
> }// onCreate
>
> @Override
> public void onSharedPreferenceChanged(SharedPreferences preferences,
> String key) {
>     if (key.equals("checkTest")) {
>         showDialog(1);
>     }
>     if (key.equals("cancel")) {
>         dismissDialog(1);
>     }
>
> }// onSPC
>
> @Override
> protected Dialog onCreateDialog(int id) {
>     switch (id) {
>     case 1: {
>         ProgressDialog dialog = new ProgressDialog(this);
>         dialog.setMessage("press back twice, start the app again, and
> click checkbox...");
>         dialog.setIndeterminate(true);
>         dialog.setCancelable(true);
>         dialog.setOnCancelListener(new OnCancelListener() {
>
>             @Override
>             public void onCancel(DialogInterface dialog) {
>                 prefs.edit().putBoolean("cancel", false).commit();
>             }
>         });
>         return dialog;
>     }// case
>     }// switch
>     return null;
>
> }// onCreateDialog
> }// PrefsActivity
>
> the prefs.xml contains just a simple checkBoxPref:
>
> <?xml version="1.0" encoding="utf-8"?>
> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/
> android">
>     <CheckBoxPreference android:key="checkTest" android:title="test" /
>
> </PreferenceScreen>
>
> and the mainfest is simple too:
>
> <?xml version="1.0" encoding="utf-8"?>
> <manifest xmlns:android="http://schemas.android.com/apk/res/android";
> package="is.it.works" android:versionCode="1"
> android:versionName="1.0">
> <uses-sdk android:minSdkVersion="4" />
>
> <application android:icon="@drawable/icon" android:label="@string/
> app_name">
>     <activity android:name=".PrefsActivity" android:label="@string/
> app_name">
>         <intent-filter>
>             <action android:name="android.intent.action.MAIN" />
>             <category android:name="android.intent.category.LAUNCHER" /
>
>         </intent-filter>
>     </activity>
>
> </application>
> </manifest>
>
> But when I run this code, got an error when leave-restart the activity
> - and checking the box:
>
> 09-14 10:34:34.472: ERROR/AndroidRuntime(281): Uncaught handler:
> thread main exiting due to uncaught exception
> 09-14 10:34:34.502: ERROR/AndroidRuntime(281):
> android.view.WindowManager$BadTokenException: Unable to add window --
> token android.os.BinderProxy@43756de8 is not valid; is your activity
> running?
>
> This error comes from the "new ProgressDialog(this)" operation.
> Changeing "this" to "getApplicationContext()", or "PrefsActivity.this"
> doesnt help, the problem is still on. It's a very simple code, but I
> can't figure out what is wrong with it (?maybe nothing, than why
> crases and what could be the solution?)...
> I'm stucked, and now I have no idea...
>
> Please, tell me why is it happens, and what could be the solution!
> Thank You Very Much!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to