我有一个ArrayAdapter类,可以创建自定义ListView项。当我尝试运行它时,我会尝试设置所有文本视图的nullpointerexception,但它们不是null。我已将它们设置为正确的ID。这是我的ArrayAdapter类
class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item_layout, null);
}
final Item p = items.get(position);
if (p != null) {
TextView likeNumberListViewItemTextView = (TextView) findViewById(R.id.like_number_listview_item_textview);
****HERE IS WHERE I GET THE NULLPOINTER****
likeNumberListViewItemTextView.setText("TEST");
}//end if null
return v;
}//end getView
}//end ListAdapter class
当视图设置为正确的ID时,如何获取nullpointer? TextView存在于xml布局中。
这是我的ListView项目的xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
<LinearLayout
android:layout_alignParentTop="true"
android:id="@+id/top_listview_item_linearlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/username_listview_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:clickable="true"
android:textSize="15sp"
android:textColor="#FFA523"
android:layout_gravity="left"/>
<TextView
android:id="@+id/like_number_listview_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:textSize="15sp"
android:textColor="#FFA523"
android:layout_gravity="right"/>
</LinearLayout>
<TextView
android:id="@+id/test_listview_item_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:textSize="17sp"
android:textColor="#454545"
android:layout_below="@id/top_listview_item_linearlayout"/>
</RelativeLayout>
这是我的LogCat错误
10-10 20:12:07.597: E/AndroidRuntime(24339): FATAL EXCEPTION: main
10-10 20:12:07.597: E/AndroidRuntime(24339): java.lang.NullPointerException
10-10 20:12:07.597: E/AndroidRuntime(24339): at com.example.MainActivity$1ListAdapter.getView(MainActivity.java:138)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.AbsListView.obtainView(AbsListView.java:2046)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.ListView.makeAndAddView(ListView.java:1820)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.ListView.fillDown(ListView.java:672)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.ListView.fillFromTop(ListView.java:732)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.ListView.layoutChildren(ListView.java:1659)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.AbsListView.onLayout(AbsListView.java:1876)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.View.layout(View.java:11425)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewGroup.layout(ViewGroup.java:4232)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.View.layout(View.java:11425)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewGroup.layout(ViewGroup.java:4232)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.View.layout(View.java:11425)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewGroup.layout(ViewGroup.java:4232)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.View.layout(View.java:11425)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewGroup.layout(ViewGroup.java:4232)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.View.layout(View.java:11425)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewGroup.layout(ViewGroup.java:4232)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1516)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.os.Looper.loop(Looper.java:154)
10-10 20:12:07.597: E/AndroidRuntime(24339): at android.app.ActivityThread.main(ActivityThread.java:4945)
10-10 20:12:07.597: E/AndroidRuntime(24339): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 20:12:07.597: E/AndroidRuntime(24339): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 20:12:07.597: E/AndroidRuntime(24339): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-10 20:12:07.597: E/AndroidRuntime(24339): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-10 20:12:07.597: E/AndroidRuntime(24339): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您正在获取NPE
,因为此行
TextView likeNumberListViewItemTextView = (TextView) findViewById(R.id.like_number_listview_item_textview);
正在返回null
。它正在查找View
的错误位置。由于您在此处item_layout.xml
充气
v = vi.inflate(R.layout.list_item_layout, null);
这是您需要查找TextView
的地方。将其更改为
TextView likeNumberListViewItemTextView = (TextView) v.findViewById(R.id.like_number_listview_item_textview);
您需要将v
添加到findViewById()
,以告知其查看虚增View
。否则,它会查看您之前layout
中setContentView()
充气的TextView
内显然无法找到此{{1}}。
答案 1 :(得分:0)
而不是findViewById(...)
您应该使用v.findViewByID(...)