Need Help on TabActivity

1.7k views Asked by At

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?

2

There are 2 answers

4
syam On BEST ANSWER

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.

0
E-Riz On

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