【发布时间】:2014-12-16 22:49:25
【问题描述】:
我在尝试使用自定义适配器填充列表视图时遇到问题。我有一个存储在 ArrayList 中的 Restaurant 对象数组,然后将这些对象传递到 ArrayAdapter。我检查了 ArrayAdapter 的内容,它确实包含我传入的对象。但是我似乎遇到的问题是,当调用 list.setAdapter(adapter) 时会引发异常。
以下代码显示了我如何尝试填充 ListView
private void populateListView() {
ArrayAdapter<Restaurants> adapter = new RestaurantsAdapter();
ListView list = (ListView) findViewById(R.id.restaurantListView);
list.setAdapter(adapter);
}
这是我的自定义适配器。它是试图显示列表的 Activity 的内部类。
private class RestaurantsAdapter extends ArrayAdapter<Restaurants> {
public RestaurantsAdapter() {
super(RestaurantsList.this,R.layout.layout_of_list_view,restaurants);
//restaurants is an object array that is filled in the outer class
}
@Override
public View getView(int position, View convertView,ViewGroup parent){
//this makes sure we have a view to work with
View v = convertView;
if(v == null) //create new view
{
v = getLayoutInflater().inflate(R.layout.layout_of_list_view, parent, false);
}
//populate the list
Restaurants r = restaurants.get(position);
TextView restName = (TextView) v.findViewById(R.id.restaurantName);
restName.setText(r.getName());
TextView desc = (TextView) v.findViewById(R.id.restaurantDescription);
desc.setText(r.getLocation());
return v;
}
}
这是描述 ListView 中的每个项目应如何显示的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restaurantImage"
android:src="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/restaurantName"
android:layout_marginLeft="89dp"
android:layout_marginTop="12dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="4"
android:text="Description"
android:id="@+id/restaurantDescription"
android:layout_marginLeft="89dp"
android:layout_marginTop="32dp"/>
虽然这是包含 ListView 本身的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:clickable="false">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restaurantListView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
代码运行时logcat显示如下错误。
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ java.lang.NullPointerException
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at project.b_ourguest.bourguest.RestaurantsList.populateListView(RestaurantsList.java:90)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at project.b_ourguest.bourguest.RestaurantsList.access$000(RestaurantsList.java:25)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at project.b_ourguest.bourguest.RestaurantsList$1.run(RestaurantsList.java:54)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-16 17:30:38.360 1839-1839/project.b_ourguest.bourguest W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
我认为问题不在发布的代码中。
-
我不太确定它还能在哪里。有什么建议吗?
-
RestaurantsList 是我正在工作的活动类。代码“RestaurantsList.class”是指上下文。
-
RestaurantList.java 中的第 90 行是什么?
标签: android android-listview android-adapter