Reputation: 5673
I implement a TabActivity exactly like this -
http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/
Now the problem is, I have some Spinner in an activity. When i click to expand the Spinner - the following exception occur -
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43b957c0 is not valid; is your activity running?
Anybody have solution?
Upvotes: 2
Views: 1719
Reputation: 445
The error might be due to the context of your adapter, try giving getParent() as the context of the adapter instead of this.
this link will help you.
Upvotes: 4
Reputation: 32914
The problem is that using "normal" layout inflation, the context used to inflate with is "this" which is the nested Activity. Unfortunately, an Activity nested inside a tab (an ActivityGroup) can't be used to show dialogs (I'm not exactly sure why that is, but it produces the BadTokenException). The solution I found most palatable is to not use setContentView(int id) but rather do explicit inflation using getParent() as the Context. Like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getParent()).inflate(R.layout.my_layout, null);
setContentView(contentView);
}
Upvotes: 4