0

我有一个创建自定义 ListView 项目的 ArrayAdapter 类。当我尝试运行它时,我会为我尝试设置的所有文本视图获得一个 nullpointerexception,但它们不为空。我已将它们设置为正确的 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 时,我如何获得空指针?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)
4

2 回答 2

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);

您需要添加vtofindViewById()以告诉它查看 inflated View。否则,它会在layout你之前膨胀的任何setContentView()地方寻找它显然不会找到 this的地方TextView

于 2013-10-11T00:37:54.343 回答
0

而不是findViewById(...)你应该使用v.findViewByID(...)

于 2013-10-11T00:32:45.273 回答