Android:Spinner上的WindowManager $ BadTockenException单击

Jom*_*mia 2 android spinner

我家里有一个旋转器.当我单击微调器时,进程停止显示捕获WindowManager $ BadTockenException的异常.

我从main.class调用这个home.class,它扩展了ActivityGroup.

如果我只是运行home.class,则微调器显示所有项目.但问题只是从main.class调用home.class.

以下是我的代码.请告诉我为什么会这样.

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);
  }
Run Code Online (Sandbox Code Playgroud)

}

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];
      }
Run Code Online (Sandbox Code Playgroud)

});

线程[<1> main](暂停(异常WindowManager $ BadTokenException))
AlertDialog(Dialog).show()行:245
AlertDialog $ Builder.show()行:802
Spinner.performClick()行:260
查看$ PerformClick.run ()行:9080
ViewRoot(Handler).handleCallback(Message)行:587 ViewRoot(Handler).dispatchMessage(Message)行:92 Looper.loop()行:123 ActivityThread.main(String [])行:3647
方法. 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 [])行:597 NativeStart.main(String [])行:不可用[native method]

谢谢....

Anj*_*nju 9

错误可能与home.class中给出的setContentView有关.

代替 setContentView(yourlayout);

给,

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

并将您的微调代码设为:

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);
Run Code Online (Sandbox Code Playgroud)

由于您使用的是活动组,因此您将面临此问题.希望这个解决方案可以帮到你.