Android: WindowManager $ BadTockenException on Spinner Click

I have a spinner in my home.class. When I click on the counter, the process stops showing an exception that is detected in WindowManager $ BadTockenException.

I call this home.class from main.class, which extends the ActivityGroup.

If I just run only home.class, then the counter displays all the elements. But the problem is only calling home.class from main.class.

Below is my code. Please tell me why this 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

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

Thanks....

+2
source share
1 answer

The error may be with setContentView specified 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 like:

 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 a group of activities, you are facing this problem. Hope this solution can help you.

+9
source

Source: https://habr.com/ru/post/1400572/


All Articles