2
votes

I have a spinner in my home.class. When I click on the spinner, the process is stopped showing exception that WindowManager$BadTockenException is caught.

I am calling this home.class from main.class which extends ActivityGroup.

If I am simply run only the home.class, the spinner is showing all items. But the problem is only with calling home.class from main.class.

The following are my code. Please tell me why this is happened.

main.class

public class main extends ActivityGroup
{
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
       Intent intent=new Intent(this,home.class);
       View view=getLocalActivityManager().startActivity("1", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
       setContentView(view);
  }

}

home.class

String[] country={"Please selects","US","INDIA","UK"};
Spinner s2 = (Spinner) findViewById(R.id.spinnerCountry);
ArrayAdapter<CharSequence> adapterCountry=new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapterCountry);

s2.setOnItemSelectedListener(new OnItemSelectedListener()
{
     public void onItemSelected( AdapterView<?> parent, View view, int position, long id)
     {
            countryName=country[position];
     }

      public void onNothingSelected(AdapterView<?> parent)
     {
            countryName=country[0];
      }

});

Stack

Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))
AlertDialog(Dialog).show() line: 245
AlertDialog$Builder.show() line: 802
Spinner.performClick() line: 260
View$PerformClick.run() line: 9080
ViewRoot(Handler).handleCallback(Message) line: 587 ViewRoot(Handler).dispatchMessage(Message) line: 92 Looper.loop() line: 123 ActivityThread.main(String[]) line: 3647
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597 NativeStart.main(String[]) line: not available [native method]

Thank You....

1

1 Answers

9
votes

The error may be with the setContentView given inside your home.class.

Instead of setContentView(yourlayout);

give,

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(yourlayout, null);
this.setContentView(viewToLoad);  
Spinner s2 = (Spinner) viewToLoad.findViewById(R.id.spinnerCountry);

And give your spinner code as:

ArrayAdapter<CharSequence> adapterCountry=new ArrayAdapter(this.getParent(),android.R.layout.simple_spinner_item,country);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapterCountry);

Since you are using activity group, you face this issue. Hope this solution may help you.