第二个TextView显示NullPointerException

时间:2015-08-28 21:02:28

标签: android listview nullpointerexception textview

我试图创建一个包含一个图像和两个textview的自定义ListView项目。它们应该水平排列(并排显示)。但是,第二个TextView始终显示为null并导致setText方法崩溃。我不确定为什么它是null,即使我已经设置它所以任何帮助都会很棒。我100%确定崩溃是NullpointerException,因为logcat是这样说的。

如果我的代码不正常,请提前抱歉,因为我刚刚开始使用Android开发。

这是我的ListView项目的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:id="@+id/category"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:paddingRight="10dp"/>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/message"
            android:layout_width="10dp"
            android:layout_height="wrap_content"
            android:textSize="19sp"
            android:textColor="#000000"
            android:textAlignment="center"
            android:gravity="left|center_horizontal"/>

        <TextView
            android:id="@+id/datetext"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:layout_toRightOf="@+id/message"
            android:textSize="19sp"
            android:textColor="#000000"
            android:textAlignment="center"
            android:gravity="center_vertical|right"/>

    </RelativeLayout>

</LinearLayout>

这是我的BaseAdapter类中的getView方法:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.custom_list_item, null);
        }

        TextView txtAlert = (TextView) view.findViewById(R.id.message);
        // This is showing up as NULL
        TextView txtDate = (TextView) view.findViewById(R.id.datetext);
        ImageView imgCategory = (ImageView) view.findViewById(R.id.category);

        // alert is a String
        txtAlert.setText(alert);  
        // date is a String; crashes here
        txtDate.setText(date);

        return view;
    }

谢谢!

编辑:如果人们还在看这篇文章,那么这里是来自logcat的堆栈跟踪。另外,有趣的是要注意我添加的任何其他TextView对象也将导致null。我也尝试过多次清理和重建项目,唉这也不行。

08-31 05:32:19.695  24080-24080/com.ryan.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.ryan.myapp.activity.NotificationsActivity$DataAdapter.getView(NotificationsActivity.java:305)
        at android.widget.AbsListView.obtainView(AbsListView.java:2604)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1253)
        at android.widget.ListView.onMeasure(ListView.java:1165)
        at android.view.View.measure(View.java:16515)
        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
        at android.view.View.measure(View.java:16515)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16515)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:720)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:592)
        at android.view.View.measure(View.java:16515)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5055)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2561)
        at android.view.View.measure(View.java:16515)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2194)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1339)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1550)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1236)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5160)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

尝试替换:

view = inflater.inflate(R.layout.custom_list_item, null);

通过

view = inflater.inflate(R.layout.custom_list_item, parent, false);

没有必要这样做

View view = convertView; 

下一步&gt;查看持有人模式:)

答案 1 :(得分:1)

我唯一能想到的是你实际上没有使用这个xml,你能检查一下吗?如果您有多个布局文件夹(对于不同的尺寸),请检查

答案 2 :(得分:0)

所以大家都感谢你的帮助!结果来自评论部分的gmetax是正确的。出于某种原因,它有一个与我给它不同的id名称。它将id的名称显示为日期,即使我将其设置为datetext。如果有人能给我一个解释,为什么会发生这种情况会很好,所以这不会在将来发生。谢谢!