自定义适配器类形成视图,但在设置列表项的文本时会给出空指针异常。
NotificatitionAdabter.java
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.Double检查了所有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);
}
}
DataSourceModel
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}}
调试
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
<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" />
适配器类的getitem方法
public MessageModel getItem(int position) {
return myList.get(position);
}
答案 0 :(得分:0)
只需更改
name=(TextView) item.findViewById(R.id.lblname);
到
name=(TextView) item.findViewById(R.id.lblSName);
答案 1 :(得分:0)
也许currentListData为null,因此在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);