滚动时自定义适配器getView中的NullPointerException

时间:2013-08-11 17:36:22

标签: android listview nullpointerexception

我正在构建一个短信应用程序,并在活动上显示用户和他的对话伙伴之间的短信列表我试图拥有一个ListView有两种不同的布局,这将代表传入和传出的短信

一切顺利,直到我尝试使用2种布局。 我可以正确地看到SMS的列表,其中有2个不同的布局用于传入\传出,因为我需要。但是当我滚动列表然后我得到一个NullPointerException。

这是我的适配器:

private class MyListAdapter extends ArrayAdapter<Sms> {
    public MyListAdapter() {
        super(ExistingConversation.this, R.layout.item_conversation_incoming, smsList);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getCount() {
        return smsList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // find the Sms to work with
        Sms currentSms = smsList.get(position);

        // Make sure we have a view to work with (may have been given null)
        View itemView = convertView;
        if (itemView == null) {
            if (currentSms.getIncomingOutgoing().equals("in")){// inflate the list for an incoming message
                itemView = getLayoutInflater().inflate(R.layout.item_conversation_incoming, parent, false);
            }
            else{ // inflate the list for an outgoing message
                itemView = getLayoutInflater().inflate(R.layout.item_conversation_outgoing, parent, false);
            }
        }

        // Fill the view:
        if (currentSms.getIncomingOutgoing().equals("in")){ //for incoming
            // Content:
            TextView contentView = (TextView) itemView.findViewById(R.id.item_content_incoming);
            contentView.setText(currentSms.getContent());

            // time:
            TextView timeView = (TextView) itemView.findViewById(R.id.item_time_incoming);
            timeView.setText(new Date(currentSms.getDate()).toString().substring(0, 19));
        }
        else{//for outgoing
            // Content:
            TextView contentView = (TextView) itemView.findViewById(R.id.item_content_outgoing);
            contentView.setText(currentSms.getContent());

            // time:
            TextView timeView = (TextView) itemView.findViewById(R.id.item_time_outgoing);
            timeView.setText(new Date(currentSms.getDate()).toString().substring(0, 19));

        }
        return itemView;
    }
}
}

这些是我的2个布局: 传入:

<?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:orientation="vertical"
android:paddingBottom="8dp"
android:paddingRight="16dp"
android:paddingTop="8dp" >

<LinearLayout
    android:id="@+id/item_container_incoming"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/list_selector_incoming"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/item_icon_incoming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:src="@drawable/social_person2" />

    <TextView
        android:id="@+id/item_content_incoming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:layout_weight="4"
        android:text="Here Goes The Content" />

    <TextView
        android:id="@+id/item_time_incoming"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:text="Small Text"
        android:textSize="12sp" />
</LinearLayout>

和传出的1是相同的,除了它写入“传入”的地方(显然是wrtien outgoing,并且在RelativeLayout的填充属性中(instaed of Right)填充它是左填充......)

另外我会在这里发布我的logCat堆栈跟踪 - 它可能有助于找到解决我烦人问题的方法:

08-11 20:18:38.707: E/AndroidRuntime(23441): FATAL EXCEPTION: main
08-11 20:18:38.707: E/AndroidRuntime(23441): java.lang.NullPointerException
08-11 20:18:38.707: E/AndroidRuntime(23441):    at com.example.smser.ExistingConversation$MyListAdapter.getView(ExistingConversation.java:356)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.AbsListView.obtainView(AbsListView.java:2452)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.ListView.makeAndAddView(ListView.java:1776)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.ListView.fillUp(ListView.java:712)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.ListView.fillGap(ListView.java:651)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5711)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3422)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:4087)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.View.dispatchTouchEvent(View.java:7380)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2464)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow. java:2151)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1480)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.app.Activity.dispatchTouchEvent(Activity.java:2487)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2099)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.View.dispatchPointerEvent(View.java:7565)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3427)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3359)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4468)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4446)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4550)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4518)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4569)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.Choreographer.doFrame(Choreographer.java:523)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.os.Handler.handleCallback(Handler.java:615)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.os.Looper.loop(Looper.java:137)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at android.app.ActivityThread.main(ActivityThread.java:4921)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at java.lang.reflect.Method.invokeNative(Native Method)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at java.lang.reflect.Method.invoke(Method.java:511)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
08-11 20:18:38.707: E/AndroidRuntime(23441):    at dalvik.system.NativeStart.main(Native Method)

所以我明天有这个截止日期,我得找到一个快速解决方案(意思是,此刻,我不太关心效率 - 我会在以后关注它)。

如果你能帮助我,那就太好了!任何澄清或问题随时发表评论我将尽我所能解释更远......

提前感谢!

更新 所以我发现由于某种原因,如果我正在评论以下if,它的确有效:

  //if (itemView == null) {
        if (currentSms.getIncomingOutgoing().equals("in")){// inflate the list for an incoming message
            itemView = getLayoutInflater().inflate(R.layout.item_conversation_incoming, parent, false);
        }
        else{ // inflate the list for an outgoing message
            itemView = getLayoutInflater().inflate(R.layout.item_conversation_outgoing, parent, false);
        }
    //}

但有些事情告诉我错了......我仍然很有兴趣听到其他一些观点...... 感谢

修改

导致执行的行是:

                contentView.setText(currentSms.getContent());

2 个答案:

答案 0 :(得分:1)

在使用之前,您是否需要初始化currentSms?如果没有,请说明哪一个是错误行

答案 1 :(得分:1)

我认为您需要在第一个View中初始化contentView,例如timeViewif..else,在那里检查itemView是否为null或不。