net.multiplesystem.nosms.displaydata.BindingData.getView中的Nullpointer异常

时间:2014-02-06 12:02:28

标签: android xml listview android-listview

package net.multiplesystem.nosms.displaydata;

import java.util.ArrayList;

import net.multiplesystem.nosms.R;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BindingData extends BaseAdapter {
 ArrayList<String> sender;
 ArrayList<String> receiver;

 LayoutInflater inflater;

 public BindingData() {

 }

 public BindingData(Activity act, ArrayList<String> sender, ArrayList<String> receiver) {
  this.sender = sender;
  this.receiver = receiver;

  inflater = (LayoutInflater) act
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

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

 @Override
 public Object getItem(int position) {
  return null;
 }

 @Override
 public long getItemId(int position) {
  return 0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  Holder holder;
  if (convertView == null) {
   holder = new Holder();
   convertView = inflater.inflate(R.layout.chatbox_listitems, null);
   holder.txtSender = (TextView) convertView.findViewById(R.id.sender);
   holder.txtReceiver = (TextView) convertView
     .findViewById(R.id.receiver);

  } else {
   holder = (Holder) convertView.getTag();
  }
  holder.txtSender.setText(Html.fromHtml( sender.get(position)));
  holder.txtReceiver.setText(Html.fromHtml(receiver.get(position)));
  return convertView;
 }

 private class Holder {
  TextView txtSender, txtReceiver;
 }
}

这里是logcat

02-06 06:50:46.840: E/AndroidRuntime(23655): FATAL EXCEPTION: main
02-06 06:50:46.840: E/AndroidRuntime(23655): Process: net.multiplesystem.nosms, PID: 23655
02-06 06:50:46.840: E/AndroidRuntime(23655): java.lang.NullPointerException
02-06 06:50:46.840: E/AndroidRuntime(23655):    at net.multiplesystem.nosms.displaydata.BindingData.getView(BindingData.java:62)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.AbsListView.obtainView(AbsListView.java:2240)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.ListView.fillDown(ListView.java:691)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.ListView.fillGap(ListView.java:655)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5136)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3236)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.AbsListView.onTouchMove(AbsListView.java:3580)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3424)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.View.dispatchTouchEvent(View.java:7690)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2066)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.app.Activity.dispatchTouchEvent(Activity.java:2457)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2014)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.View.dispatchPointerEvent(View.java:7870)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3919)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3808)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3510)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3567)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3456)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3425)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3433)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3406)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5520)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5500)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5471)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5594)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.os.MessageQueue.nativePollOnce(Native Method)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.os.MessageQueue.next(MessageQueue.java:137)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.os.Looper.loop(Looper.java:124)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at android.app.ActivityThread.main(ActivityThread.java:4998)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at java.lang.reflect.Method.invokeNative(Native Method)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at java.lang.reflect.Method.invoke(Method.java:515)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-06 06:50:46.840: E/AndroidRuntime(23655):    at dalvik.system.NativeStart.main(Native Method)

基本上我将XML文件数据显示到listview中。数据从XML文件中成功获取并显示在listview中。但是在滚动listview数据时应用程序停止了。我不知道。问题出在BindingData类的getView方法中。请帮我。提前完成。

3 个答案:

答案 0 :(得分:2)

问题在于您的实施:

 @Override
 public Object getItem(int position) {
  return null;
 }

 @Override
 public long getItemId(int position) {
  return 0;
 }

更改它以返回实际数据:

 @Override
 public Object getItem(int position) {
  return sender.get(position);
 }

 @Override
 public long getItemId(int position) {
  return position;
 }

其余的由laatlo回答。

答案 1 :(得分:1)

您忘记使用setTag()存储持有人。持有人将null获得回收的观点。添加

convertView.setTag(holder);

convertView == null分支。

另外,请确保您的chatbox_listitems布局实际上包含ID为senderreceiver的视图。

此外,您有两个构造函数,其中只有另一个构造函数初始化inflatersenderreceiver。移除另一个以确保您不会意外地使用它,尽管这不是这个NPE的原因(它会在之前发生)。

一般来说,如果你在调试器中单步执行代码,这样的NPE很容易理解。

答案 2 :(得分:0)

尝试替换

  ArrayList<String> sender;
     ArrayList<String> receiver;

通过

 ArrayList<String> sender=  new ArrayList<String>();
     ArrayList<String> receiver=  new ArrayList<String>();