我想在程序中显示ListView
视图。
我创建了一个activity
,在onCreate()
方法中运行了一个AsyncTask
,并打算在onPostExecute()
方法中显示listView
。
下面是代码片段
protected void onPostExecute(KpiResponseObject kpiReportResponse) {
ListView listView;
listView = (ListView) findViewById(R.id.list_view);
}
但是listView
对象获得是null值。
我不知道是不是context
的问题,系统也没有明确的指出来。
是代码的问题吗?
由于Android不允许在新线程中访问Activity里的界面组件,所以你的listView是null.
可通过AsyncTask的构造方法传入listView,然后在onPostExecute操作listView
你在 setContentView()
中放置的布局没有 id 是 list_view
的视图。
如果硬是要在其他线程或者类中修改UI,你可以尝试传送context来解决。获取控件的时候要用以下方式实现:
listcontainer=LayoutInflater.from(context);
view=listcontainer.inflate(R.layout.xxx,null)
(ListView)view.findViewById(R.id.xxx);