列表视图的数组适配器抛出空指针异常
我正在尝试在视图数组中添加列表视图。 lv1.setAdapter 抛出空指针异常。我不知道为什么会这样。
这是我的代码:
private ListView lv1;
private String lv_arr[]= new String[]{"Android","iPhone","BlackBerry","AndroidPeople"};
View[] vs = new View[1];
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String> (MyCalendar.this,android.R.layout.simple_list_item_1,lv_arr));
vs[0] = lv1;
view.setListViewItems(vs);
这是 list.xml 中的 ListViewO1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I am trying to add list view in an array of Views. The null pointer exception is thrown at lv1.setAdapter. I don't know why it's happening.
Here is my code:
private ListView lv1;
private String lv_arr[]= new String[]{"Android","iPhone","BlackBerry","AndroidPeople"};
View[] vs = new View[1];
lv1=(ListView)findViewById(R.id.ListView01);
lv1.setAdapter(new ArrayAdapter<String> (MyCalendar.this,android.R.layout.simple_list_item_1,lv_arr));
vs[0] = lv1;
view.setListViewItems(vs);
here is the ListViewO1 inside list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否将内容视图设置为list.xml?
接受正确的答案就是正确的,更多的人可能会帮助你!
(这是帖子左侧的灰色勾号(位于评分数字下方),单击它,它会变成绿色以接受答案)。
are you setting your content view to list.xml?
Accept correct answers as correct and more people are likely to help you!
( it's the greyed out tick on the left of a post (below the rating number), click it and it will go green to accept the answer ).