【问题标题】:Android :WindowManager$BadTockenException on Spinner ClickAndroid :WindowManager$BadTockenException on Spinner Click
【发布时间】:2011-01-12 14:51:28
【问题描述】:

我的 home.class 中有一个微调器。当我点击微调器时,进程停止显示 WindowManager$BadTockenException 被捕获的异常。

我从扩展 ActivityGroup 的 main.class 调用这个 home.class。

如果我只运行 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);
  }

}

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

});

堆栈

线程 [ 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
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本机方法]
Method.invoke(Object, Object...) 行:507
ZygoteInit$MethodAndArgsCaller.run() 行:839
ZygoteInit.main(String[]) 行:597 NativeStart.main(String[]) 行:不可用 [本机方法]

谢谢……

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    错误可能与您的 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);
    

    并给你的微调器代码:

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

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

    【讨论】:

    • 这救了我,还需要注意的是你仍然需要将 getParent() 传递给你的 AlertDialogs 等等......但这肯定会解决 Spinner 问题......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多