2012-12-15 44 views
0

在我的應用程序中,我使用Android自定義視圖來顯示一些日誌信息。在這個觀點中,我想根據一些標準來改變項目的顏色。我試着用下面的代碼在getView中導致NullPointerException。Android自定義列表視圖中的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) 

請幫我解決這個問題

感謝

+0

當你的應用程序崩潰時,你應該總是發佈你的LogCat,但我仍然猜測。 – Sam

+0

爲什麼你在異常塊外的'getView()'中返回null, - 味道不好!至於NPE本身,你確定'layoutResouceId'是'row'的實際資源,**應該是非空嗎?如果是這樣的話,那麼NPE來自其他地方 - logcat請! – t0mm13b

回答

1

我敢打賭,status爲空:

if(-1 != item.status.indexOf("OK")) 

item顯然不是零和假設holder.txtStatus.setText()不要麼拋出NPE,這只是葉status ...

(但沒有更多的信息我無法超越此範圍。)

+0

我用null狀態檢查器和logcat修改了我的問題 – Riskhan

+0

你在測試這個API嗎? – Sam

+0

listview的輸入數據來自數據庫。我檢查了狀態的值,它不爲空。我改變了檢查null的值。現在它正在工作,無論如何感謝 – Riskhan