-1

我正在构建一个 SMS 应用程序,并在显示用户与其对话伙伴之间的 SMS 列表的活动上,我试图ListView使用 2 种不同的布局,这将代表传入和传出的 SMS。

在我尝试使用 2 种布局之前,一切都很好。我可以根据需要使用 2 种不同的传入\传出布局正确查看 SMS 列表。但是当我滚动列表时,我得到一个 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填充的 ' 属性中(而不是右填充它是左填充......)

此外,我将在此处发布我的 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());
4

2 回答 2

1

使用前不需要初始化currentSms吗?如果不是,请指出哪一个是错误行

于 2013-08-11T17:45:43.337 回答
1

我认为您需要在第一个检查是否存在的地方初始化Views ,例如contentView, 。timeViewif..elseitemViewnull

于 2013-08-11T17:57:42.730 回答