【问题标题】:Unable to add window -- token null is not for an application无法添加窗口 - 令牌 null 不适用于应用程序
【发布时间】:2012-11-30 10:15:52
【问题描述】:

嗨,我正在尝试从视图 (A.xml)mainActivity 中的 AsyncTask 的 onPostExecute() 膨胀视图 (B.xml)(其中包含微调器)

MainActivity.class

protected void onPostExecute(String result) {
        LayoutInflater vi = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = vi.inflate(R.layout.B, null);
        branded_spinner = (Spinner) v.findViewById(R.id.Spinner01);

        adapter_branded = new ArrayAdapter<String>(**MainActivity.this**,
                android.R.layout.simple_spinner_item, Branded);
        adapter_branded.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        branded_spinner.setAdapter(adapter_branded);                        
        branded_spinner.setOnItemSelectedListener(**MainActivity.this**);

}

它适用于 4.0 及更高版本

但对于 2.3.3 我得到一个错误

11-30 15:28:55.492: E/AndroidRuntime(540): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-30 15:28:55.492: E/AndroidRuntime(540):  at android.view.ViewRoot.setView(ViewRoot.java:531)
11-30 15:28:55.492: E/AndroidRuntime(540):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-30 15:28:55.492: E/AndroidRuntime(540):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

是的,我尝试使用 getApplicationContext() ,但仍然是同样的错误

我提到了

Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context

Android: ProgressDialog.show() crashes with getApplicationContext

Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

【问题讨论】:

  • 点击微调器时出现错误
  • 您确定可以使用ApplicationContext 进行绘图吗?您是否尝试过通过LayoutInflater.from(context) 静态构造函数获取LayoutInflaterobject 实例(当context 是您活动的上下文时)?
  • 是的,一切都在给定的 sn-p i hv 中
  • 好的,尝试为你使用这样的构造函数vi变量:LayoutInflater vi = LayoutInflater.from(YourActivityName.this);
  • 是的,它有效..感谢 Evos 长期以来一直坚持...

标签: android dialog android-asynctask spinner layout-inflater


【解决方案1】:

好的,尝试为你使用这样的构造函数vi 变量:LayoutInflater vi = LayoutInflater.from(YourActivityName.this);

【讨论】:

    【解决方案2】:

    你只能传递 LayoutInflater.from(this);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 2019-01-04
      • 2011-12-17
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多