在我的应用程序中,我使用 Android 自定义视图来显示一些日志信息。在该视图中,我想根据某些标准更改项目的颜色。我尝试使用以下代码导致 getView 中出现 NullPointerException。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
View row = convertView;
ItemHolder holder = null;
if( row == null ){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResouceId, parent, false);
holder = new ItemHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtPhoneno = (TextView)row.findViewById(R.id.lvphoneno);
holder.txtDateTime = (TextView)row.findViewById(R.id.lvdatetime);
holder.txtSerialno = (TextView)row.findViewById(R.id.lvserialno);
holder.txtStatus = (TextView)row.findViewById(R.id.lvstatus);
row.setTag(holder);
}
else {
holder = (ItemHolder)row.getTag();
}
MyListViewItem item = items.get(position);
holder.imgIcon.setImageResource(item.icon);
holder.txtPhoneno.setText(item.phoneno);
holder.txtDateTime.setText(item.datetime);
holder.txtSerialno.setText(item.serialno);
if( !item.status.isEmpty()){
if( -1 != item.status.indexOf("OK"))
holder.txtStatus.setTextColor(Color.GREEN);
else
holder.txtStatus.setTextColor(Color.RED);
}
holder.txtStatus.setText(item.status);
return row;
}
catch (Exception e) {
Helper.Log(getContext(), e.toString());
}
return null;
}
但有趣的是,当我删除下面的行时它工作正常
if( !item.status.isEmpty()){
if( -1 != item.status.indexOf("OK"))
holder.txtStatus.setTextColor(Color.GREEN);
else
holder.txtStatus.setTextColor(Color.RED);
}
日志猫
12-15 07:04:49.668: E/AndroidRuntime(331): FATAL EXCEPTION: main
12-15 07:04:49.668: E/AndroidRuntime(331): java.lang.NullPointerException
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.AbsListView.obtainView(AbsListView.java:1432)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.ListView.makeAndAddView(ListView.java:1745)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.ListView.fillDown(ListView.java:670)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.ListView.fillSpecific(ListView.java:1302)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.ListView.layoutChildren(ListView.java:1576)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.os.Handler.dispatchMessage(Handler.java:99)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.os.Looper.loop(Looper.java:123)
12-15 07:04:49.668: E/AndroidRuntime(331): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-15 07:04:49.668: E/AndroidRuntime(331): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 07:04:49.668: E/AndroidRuntime(331): at java.lang.reflect.Method.invoke(Method.java:507)
12-15 07:04:49.668: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-15 07:04:49.668: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-15 07:04:49.668: E/AndroidRuntime(331): at dalvik.system.NativeStart.main(Native Method)
请帮我解决这个问题
谢谢