Java 自定义适配器中的SetText NullPointerException
自定义适配器类形成一个视图,但在设置列表项的文本时会出现空指针异常 notificationadabter.javaJava 自定义适配器中的SetText NullPointerException,java,android,baseadapter,settext,Java,Android,Baseadapter,Settext,自定义适配器类形成一个视图,但在设置列表项的文本时会出现空指针异常 notificationadabter.java public View getView(int position, View convertView, ViewGroup parent) { ViewHolder mViewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.row, parent
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, parent, false);
mViewHolder = new ViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
MessageModel currentListData = getItem(position);
Log.i(tag,mViewHolder.source.getText().toString());
try {
mViewHolder.name.setText(currentListData.getName());
mViewHolder.source.setText(currentListData.getSource());
Log.i(tag, currentListData.getName());
mViewHolder.destination.setText(currentListData.getDestination());
mViewHolder.message.setText(currentListData.getMessage());
mViewHolder.time.setText(currentListData.getTime());
}
catch(Exception e){}//Log.i(tag,"4");
if(convertView==null)
Log.i(tag,"null");
else
Log.i(tag,"not null");
return convertView;
}
gettext也适用于上面的视图,但setText提供NPE。仔细检查所有xml文件是否有任何打字错误
public class ViewHolder {
TextView name;
TextView source;
TextView destination;
TextView message;
TextView time;
public ViewHolder(View item) {
name=(TextView) item.findViewById(R.id.lblname);
destination=(TextView) item.findViewById(R.id.lblDestination);
message=(TextView) item.findViewById(R.id.lblSCustMsg);
time=(TextView) item.findViewById(R.id.lblSTime);
source=(TextView) item.findViewById(R.id.lblSSource);
}
}
数据源模型
public class MessageModel {
String source,destination,custmsg,time,name;
public MessageModel(String source,
String destination,String custmsg,
String time,String name)
{
this.source=source;
this.destination=destination;
this.custmsg=custmsg;
this.time=time;
this.name=name;
}
public String getName()
{
return name;
}
public String getSource()
{
return source;
}
public String getTime()
{
return time;
}
public String getMessage()
{
return custmsg;
}
public String getDestination()
{
return destination}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSName"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSTime"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSSource"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSDestination"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSCustMsg"
android:text="item" />
调试
4-17 23:47:19.378 23874-23874/com.example.abc.myapplication E/AndroidRuntime:FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.abc.myapplication.NotificationAdapter.getView(NotificationAdapter.java:76)
at android.widget.AbsListView.obtainView(AbsListView.java:2340)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
at android.widget.ListView.onMeasure(ListView.java:1162)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15264)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4918)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2161)
at android.view.View.measure(View.java:15264)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2131)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1242)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1127)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4609)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
at android.view.Choreographer.doCallbacks(Choreographer.java:567)
at android.view.Choreographer.doFrame(Choreographer.java:536)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:4987)
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:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)
row.xml
public class MessageModel {
String source,destination,custmsg,time,name;
public MessageModel(String source,
String destination,String custmsg,
String time,String name)
{
this.source=source;
this.destination=destination;
this.custmsg=custmsg;
this.time=time;
this.name=name;
}
public String getName()
{
return name;
}
public String getSource()
{
return source;
}
public String getTime()
{
return time;
}
public String getMessage()
{
return custmsg;
}
public String getDestination()
{
return destination}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSName"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSTime"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSSource"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSDestination"
android:text="item" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lblSCustMsg"
android:text="item" />
换衣服
name=(TextView) item.findViewById(R.id.lblname);
进入
可能currentListData为空,因此在setText之前添加
if(currentListData!=null)
另外,findViewById()中的名称和目的地不正确。
你必须改变:
name=(TextView) item.findViewById(R.id.lblname);
into:
name=(TextView) item.findViewById(R.id.lblSName);
destination(TextView)item.findViewById(R.id.lblDestination);
into:
destination=(TextView) item.findViewById(R.id.lblSDestination);
如果我告诉您
currentListData
为空会怎样?是否可能currentListData
为null
,即getItem()
方法可以返回null
值?如果是这样,这可能解释了NPE.currentlistData.getName()。打印为调试消息您是否确定所有这些TextView
s都在行
布局中?lblname
和lbldestinion
不在行
布局中。它们应该是ViewHolder
构造函数中的lblSName
和lblSDestination
。以及lbldestinition