您好,我正在尝试从AsyncTask的onPostExecute()方法中膨胀一个视图(B.xml),该视图中包含一个下拉列表框,以便在MainActivity的视图(A.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)
静态构造函数获取LayoutInflater
对象实例吗(其中context
是你的活动的上下文)? - Evosvi
变量:LayoutInflater vi = LayoutInflater.from(YourActivityName.this);
- Evos