9

I have an activity group containing 3 activities. When a button is pressed, I enter into this activity group and show the 1st activity. From the 1st activity I can goto 2nd activity and from 2nd activity I can goto 3rd activity.

I have a spinner in this 3rd activity layout. Problem is I am not able to click on that spinner. Error gets displayed showing:

12-31 11:29:41.082: ERROR/AndroidRuntime(474): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43791b18 is not valid; is your activity running?

How can I solve this issue? Can anyone plz help...

Hi,

Please find the code for spinner attached:

setContentView(R.layout.requestinfo);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            PGDealerInfoRequestActivity.this, R.array.request_options, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);

Inside requestinfo.xml,

<Spinner android:id="@+id/spinner" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:textColor="@android:color/darker_gray"
            android:textSize="12sp" android:textStyle="bold"
            android:layout_marginLeft="10dp" android:layout_marginTop="8dp" />
Anju
  • 9,379
  • 14
  • 55
  • 94

4 Answers4

44

The error was with the setContentView. I had given

setContentView(R.layout.mylayout);

Instead of that we should give,

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.mylayout, null);
this.setContentView(viewToLoad);  

And the spinner code is:

Spinner spinner = (Spinner) findViewById(R.id.spinner);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.request_options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
Anju
  • 9,379
  • 14
  • 55
  • 94
  • 2
    Thanks i have same problem and your answer has helped me out from my problem – Dharmendra Jul 04 '11 at 08:38
  • Thanks i have same problem .But couldn't fix it...Please help me http://stackoverflow.com/questions/7609519/android-spinner-error-android-view-windowmanagerbadtokenexception-unable-to-a – Piraba Sep 30 '11 at 11:48
  • In this way fix the spinner issue,but if i have a button on the layout and that button click event is not recognized,any idea how to fix that issue – Sam Oct 24 '11 at 13:20
  • thanks, worked for me too. but what on eart is the reason to use a layoutinflater instead of setting the contentview in the way i did severeal times so far with setContentView(R.id.mylayout)??? – Elias Feb 05 '13 at 16:32
1

can you add spiner.setDropDownViewResource()?and in your initialization ,you use context called this or getApplicationContext(),for example AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent())

pengwang
  • 19,536
  • 34
  • 119
  • 168
0

Mathew his method works:

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.mylayout, null);
this.setContentView(viewToLoad);  

i folowed this method to and then my application crashes on button click. To solve this remove the onClick method out the xml file. Go to the java class and add button.setOnClickListener!

gkenny
  • 51
  • 7
0

this may solve your problem this.getParent() i used it in my code many times. it worked f9.

N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • 1
    Added...but for spinners alone I m not able to do...other dialogs are coming...same error now also... :( – Anju Jan 05 '11 at 10:28