我正在尝试使用两种布局模拟简单的聊天视图,一种用于发送,另一种用于接收消息。当我尝试滚动时,抛出NullPointerException。不知道为什么会这样。有人能找到问题。
public class DisplayMessageAdapter extends ArrayAdapter<Message> {
Context context;
int sentResource, rcvdResource;
ArrayList<Message> messages = null;
public DisplayMessageAdapter(Context context, int sentResource, int rcvdResource, ArrayList<Message> messages) {
super(context, sentResource, rcvdResource, messages);
this.context = context;
this.sentResource = sentResource;
this.rcvdResource = rcvdResource;
this.messages = messages;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MessagesHolder holder = null;
Message message = messages.get(position);
if(message.sent != null)
{
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(sentResource, parent, false);
holder = new MessagesHolder();
holder.sent = (TextView) row.findViewById(R.id.sent_message);
row.setTag(holder);
}
else
{
//Log.i("holder-not-null","holder-not-null");
holder = (MessagesHolder) row.getTag();
}
holder.sent.setText(message.sent);
}
else
{
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(rcvdResource, parent, false);
holder = new MessagesHolder();
holder.received = (TextView) row.findViewById(R.id.received_message);
row.setTag(holder);
}
else
{
//Log.i("holder-not-null","holder-not-null");
holder = (MessagesHolder) row.getTag();
}
holder.received.setText(message.received);
}
return row;
}
static class MessagesHolder
{
TextView sent;
TextView received;
} }
sent_message和received_message布局
<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="fill_parent"
>
<TextView
android:id="@+id/sent_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:background="@drawable/sent"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:textSize="15sp"
/>
</RelativeLayout>
<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="fill_parent"
>
<TextView
android:layout_alignParentRight="true"
android:id="@+id/received_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rcvd"
android:layout_marginRight="20dp"
android:textColor="#000000"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:textSize="15sp"
/>
</RelativeLayout>
logcat的
12-23 13:46:04.996: E/AndroidRuntime(8131): FATAL EXCEPTION: main
12-23 13:46:04.996: E/AndroidRuntime(8131): java.lang.NullPointerException
12-23 13:46:04.996: E/AndroidRuntime(8131): at com.example.swipetext.DisplayMessageAdapter.getView(DisplayMessageAdapter.java:58)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.AbsListView.obtainView(AbsListView.java:2608)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.ListView.makeAndAddView(ListView.java:1852)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.ListView.fillUp(ListView.java:718)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.ListView.fillGap(ListView.java:655)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6592)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3706)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.widget.AbsListView.onTouchEvent(AbsListView.java:4556)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.View.dispatchTouchEvent(View.java:7817)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2159)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2441)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2174)
12-23 13:46:04.996: E/AndroidRuntime(8131): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2326)
12-23 13:46:04.996: E/AndroidRuntime(8131): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1612)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.app.Activity.dispatchTouchEvent(Activity.java:2494)
12-23 13:46:04.996: E/AndroidRuntime(8131): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2274)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.View.dispatchPointerEvent(View.java:8025)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4659)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4547)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4198)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4167)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4252)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4175)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4309)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4198)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4167)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4175)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4148)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6293)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6231)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6202)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6373)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:177)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6346)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6392)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.Choreographer.doFrame(Choreographer.java:559)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.os.Handler.handleCallback(Handler.java:730)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.os.Looper.loop(Looper.java:137)
12-23 13:46:04.996: E/AndroidRuntime(8131): at android.app.ActivityThread.main(ActivityThread.java:5419)
12-23 13:46:04.996: E/AndroidRuntime(8131): at java.lang.reflect.M
调用适配器的代码
public void populateView(ArrayList<Message> allMessages)
{
DisplayMessageAdapter adapter = new DisplayMessageAdapter(getApplicationContext() , R.layout.sent_message_layout, R.layout.received_message_layout, allMessages);
listview = (ListView) findViewById(R.id.messageview);
listview.setAdapter(adapter);
listview.setSelection(listview.getAdapter().getCount()-1);
}
更改后
public class DisplayMessageAdapter extends ArrayAdapter<Message> {
Context context;
int sentResource, rcvdResource;
ArrayList<Message> messages = null;
public DisplayMessageAdapter(Context context, int sentResource, int rcvdResource, ArrayList<Message> messages) {
super(context, sentResource, rcvdResource, messages);
this.context = context;
this.sentResource = sentResource;
this.rcvdResource = rcvdResource;
this.messages = messages;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MessagesHolder holder = null;
Message message = messages.get(position);
if(message.sent != null)
{
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(sentResource, parent, false);
holder = new MessagesHolder();
holder.data = (TextView) row.findViewById(R.id.sent_message);
row.setTag(holder);
}
else
{
//Log.i("holder-not-null","holder-not-null");
holder = (MessagesHolder) row.getTag();
}
holder.data.setText(message.sent);
}
else
{
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(rcvdResource, parent, false);
holder = new MessagesHolder();
holder.data = (TextView) row.findViewById(R.id.received_message);
row.setTag(holder);
}
else
{
//Log.i("holder-not-null","holder-not-null");
holder = (MessagesHolder) row.getTag();
}
holder.data.setText(message.received);
}
return row;
}
static class MessagesHolder
{
TextView data;
}
}
答案 0 :(得分:1)
您忘记覆盖以下方法
@Override
public int getCount() {
// TODO Auto-generated method stub
return messages.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
答案 1 :(得分:0)
可能会在这些地方引起空指针异常 -
holder.sent.setText(message.sent);
holder.received.setText(message.received);
这是因为,第一次基于messages.sent.Suppose给你的行布局充气。它是一个已发送的消息,所以你为发送的消息膨胀行布局,初始化MessagesHolder的已发送文本视图。当你开始滚动,视图不为空,因此它们不会被重绘,所以此时收到的消息进入先前被发送消息占据的位置,它尝试在textView holder.recived上设置文本,该文本尚未初始化,并且导致空指针异常。
溶液 - 在MessagesHolder类中只使用一行布局和一个文本视图成员。
答案 2 :(得分:0)
Temproary Solution:
这个问题似乎存在于持有者中并且正在使用中。如果我不使用持有者类来提升性能并进行天真的实现,它现在可以正常工作。虽然我不确定如何正确使用支架,但如果需要性能提升。