【发布时间】: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() ,但仍然是同样的错误
我提到了
Android: ProgressDialog.show() crashes with getApplicationContext
【问题讨论】:
-
点击微调器时出现错误
-
您确定可以使用
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