私のアプリでは、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);
}
logcat
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)
この問題を解決するのを手伝ってください
ありがとう