How to fix this WindowManager$BadTokenException

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to fix this WindowManager$BadTokenException

Postby ranjandeo » Mon Aug 02, 2010 1:07 pm

Hello friends.

When I am trying to click on a spinner which is in an activity inside another activity group in a tab, I am getting the following exception.

I have goggled the issue a lot but couldn't find any solution.
If any body has some idea then please help me out.

[list=]08-02 17:30:57.797: ERROR/AndroidRuntime(320): Uncaught handler: thread main exiting due to uncaught exception
08-02 17:30:57.817: ERROR/AndroidRuntime(320): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44ed8338 is not valid; is your activity running?
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewRoot.setView(ViewRoot.java:468)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.Dialog.show(Dialog.java:239)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.widget.Spinner.performClick(Spinner.java:257)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.View.onTouchEvent(View.java:4179)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.View.dispatchTouchEvent(View.java:3709)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.os.Looper.loop(Looper.java:123)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at java.lang.reflect.Method.invoke(Method.java:521)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-02 17:30:57.817: ERROR/AndroidRuntime(320): at dalvik.system.NativeStart.main(Native Method)
[/list]
Last edited by ranjandeo on Thu Aug 05, 2010 4:19 am, edited 1 time in total.
Ranjan Deo
User avatar
ranjandeo
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon May 31, 2010 6:20 am
Location: Kolkata, India

Top

Re: How to fix this WindowManager$BadTokenException

Postby starazam » Wed Aug 04, 2010 12:53 pm

I am facing with exactly same problem. Please help
starazam
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 04, 2010 12:05 pm

Re: How to fix this WindowManager$BadTokenException

Postby blundell » Wed Aug 04, 2010 2:59 pm

Can't help, but just out of interest what is your package name?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: How to fix this WindowManager$BadTokenException

Postby starazam » Wed Aug 04, 2010 5:43 pm

Can't get what you mean by Package Name, if this is My code package name, then I am using "com.android.myfirstapp".

basically I am using tabs, but because I have more than one activity to show in a single tab, I am following this article

http://blog.henriklarsentoft.com/2010/0 ... ctivities/

to create activitygroup and sub activities. all working. but I have a spinner control in a sub activity, when I try to open spinner by clicking it, it producing above mentioned exception.

this code is working perfectly when I put it in main activity, outside tabs.

ArrayAdapter<CharSequence> adapterState = ArrayAdapter.createFromResource(
this, R.array.states_list, android.R.layout.simple_spinner_item);
adapterState.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnState.setAdapter(adapterState);

basically this is some context problem. I have already tried getApplicationContext()
and ProfileGroup.groupContext

while ProfileGroup is name of ActivitiyGroup Class, and groupContext is static variable of type Context, holding the instance of ProfileGroup Class.

let me know, if you didn't get me.......I must need to solve this.....


Regards,
Azam.
starazam
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 04, 2010 12:05 pm

Re: How to fix this WindowManager$BadTokenException

Postby jh73 » Wed Aug 04, 2010 10:50 pm

I am having this exact problem as well. I can't seem to put a working Spinner inside sub activities. Any help would be appreciated.
jh73
Freshman
Freshman
 
Posts: 2
Joined: Wed Aug 04, 2010 10:47 pm

Re: How to fix this WindowManager$BadTokenException

Postby ranjandeo » Thu Aug 05, 2010 4:16 am

blundell wrote:Can't help, but just out of interest what is your package name?

Hi,

Does package name make any difference ?
I don't think package name is related to this issue any more.

But your posts in the community indicate that you are having much more experience and if u think that package name plays any role here, so let me know.

Thank you.
Ranjan Deo
User avatar
ranjandeo
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon May 31, 2010 6:20 am
Location: Kolkata, India

Top

Re: How to fix this WindowManager$BadTokenException

Postby starazam » Thu Aug 05, 2010 4:59 am

its strange, no body knows about it, while I was thinking this must be very common problem.....

well, I am going to give try ViewSwitcher/ViewFlipper, instead of using Sub Activities, so I can continue progress in my work. but I will keep searching for this.

Android Tabs have so many limitations, in Iphone you have so many options......

Regards,
Azam.
starazam
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 04, 2010 12:05 pm

Re: How to fix this WindowManager$BadTokenException

Postby ranjandeo » Thu Aug 05, 2010 7:07 am

starazam wrote:its strange, no body knows about it, while I was thinking this must be very common problem.....

well, I am going to give try ViewSwitcher/ViewFlipper, instead of using Sub Activities, so I can continue progress in my work. but I will keep searching for this.

Android Tabs have so many limitations, in Iphone you have so many options......

Regards,
Azam.


Yes Azam you are right, Android tab has so many limitations.
In Iphone you don't need to put any extra effort for this.

You said here that you will use ViewSwithcer/ViewFlipper in your scenario now.
Can you please expalin me how this can cater our need ?
Since I don't have idea about these two controls, if it is possible for you then plz make it clear to me.

Thank You
Ranjan Deo
User avatar
ranjandeo
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon May 31, 2010 6:20 am
Location: Kolkata, India

Re: How to fix this WindowManager$BadTokenException

Postby pskink » Thu Aug 05, 2010 9:03 am

try this...
Attachments
Prefs.tar.gz
(6.25 KiB) Downloaded 1804 times
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: How to fix this WindowManager$BadTokenException

Postby ranjandeo » Thu Aug 05, 2010 10:00 am

pskink wrote:try this...


Thanks pskink,

Really you are a master developer :-)
I tested your code it is working fine.

Thanks :-)
Ranjan Deo
User avatar
ranjandeo
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Mon May 31, 2010 6:20 am
Location: Kolkata, India

Re: How to fix this WindowManager$BadTokenException

Postby starazam » Thu Aug 05, 2010 12:47 pm

thanks, great work pskink, I think, key point in your code is, use
View v=LayoutInflater.from(context).inflate(R.layout.spinner, null);
.
//work with view elements
.
setContentView(v);

instead setContentView(R.layout.spinner);
starazam
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 04, 2010 12:05 pm

Re: How to fix this WindowManager$BadTokenException

Postby starazam » Thu Aug 05, 2010 12:53 pm

ranjan

ViewFlipper is class to flip views on same activity, please check

http://www.warriorpoint.com/blog/2009/0 ... ewflipper/

in the example, he use only next(), prev() functionality, however, I think we can navigate directly to view, by passing its index.

check http://developer.android.com/reference/ ... dChild(int)

that was good possible alternative, if pskink has not helped us....
starazam
Freshman
Freshman
 
Posts: 5
Joined: Wed Aug 04, 2010 12:05 pm

Re: How to fix this WindowManager$BadTokenException

Postby jh73 » Thu Aug 05, 2010 4:12 pm

pskink wrote:try this...

Thanks, that was the solution I was looking for!
jh73
Freshman
Freshman
 
Posts: 2
Joined: Wed Aug 04, 2010 10:47 pm

Re: How to fix this WindowManager$BadTokenException

Postby rose_r_2011 » Fri May 20, 2011 2:56 am

thinks pskink, your code helps me very much
:)
rose_r_2011
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 20, 2011 2:52 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 4 guests