[android-developers] Re: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity

2011-09-19 Thread danielkr
Maybe you get the error because the program hasn't exited it Perhaps
you need onPause and onResume or onExit or onRestart methods to handle
this correctly.


On Sep 14, 12:38 pm, lama  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:
>
> 
> http://schemas.android.com/apk/res/
> android">
>     
> 
>
> and the mainfest is simple too:
>
> 
> http://schemas.android.com/apk/res/android";
> package="is.it.works" android:versionCode="1"
> android:versionName="1.0">
> 
>
> 
>     
>         
>             
>             
>         
>     
>
> 
> 
>
> 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


[android-developers] Re: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity

2011-09-15 Thread lama
Hi Vivek!

Thank You for posting!
I tried what You suggesed, but the problem is still on. Setting
postDelayed() executing to 5 sec, and the app is still crashing. So I
think the problem isn't based on timeing, the activity started after
300-400 millisecs, but system doesn't find it, or something like this
could happening...
If You'll have any other idea, I'll try it; that would be big help to
me!

Thanks,
lama

On Sep 15, 11:36 am, Vivek975  wrote:
> 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  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:
>
> > 
> > http://schemas.android.com/apk/res/
> > android">
> >     
> > 
>
> > and the mainfest is simple too:
>
> > 
> > http://schemas.android.com/apk/res/android";
> > package="is.it.works" android:versionCode="1"
> > android:versionName="1.0">
> > 
>
> > 
> >     
> >         
> >             
> >             
> >         
> >     
>
> > 
> > 
>
> > 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


[android-developers] Re: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity

2011-09-15 Thread Vivek975
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  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:
>
> 
> http://schemas.android.com/apk/res/
> android">
>     
> 
>
> and the mainfest is simple too:
>
> 
> http://schemas.android.com/apk/res/android";
> package="is.it.works" android:versionCode="1"
> android:versionName="1.0">
> 
>
> 
>     
>         
>             
>             
>         
>     
>
> 
> 
>
> 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