2011-04-06 58 views
0

我試圖採取從ViewHolder去年穀歌IO羅曼蓋伊的建議的列表,以優化列表視圖從例如: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ (感謝管理員) 但每當我試着翻轉列表或甚至使其移動一點,應用程序崩潰,發生了什麼事?任何幫助,將不勝感激:) 這裏是我做過什麼>試圖優化ListView和失敗

private class OrderAdapter extends ArrayAdapter<Order> { 
    private ArrayList<Order> items; 
    LayoutInflater mInflater; 

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) { 
     super(context, textViewResourceId, items); 
     mInflater = LayoutInflater.from(context); 
     this.items = items; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.row, parent, false); 
      holder = new ViewHolder(); 
      holder.tt = (TextView) convertView.findViewById(R.id.toptext); 
      holder.bt = (TextView) convertView.findViewById(R.id.bottomtext); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     Order o = items.get(position); 
     if (o != null) { 
      if (holder.tt != null) { 
       holder.tt.setText("Name: " + o.getOrderName()); 
      } 
      if (holder.bt != null) { 
       holder.bt.setText("Status: " + o.getOrderStatus()); 
      } 
     } 
     return convertView; 
    } 
} 

這是錯誤消息我得到了,對不起,我不知道如何格式化>

致命異常:主要 的java .lang.NullPointerException at com.softberries.lve.SoftwarePassionView $ OrderAdapter.getView(SoftwarePassionView.java:98) at android.widget.AbsListView.obtainView(AbsListView.java:1294) at android.widget.ListView.makeAndAddView( ListView.java:1727) at android.widget.ListView.fillDown(ListView.java:652) 在android.widget.ListView.fillGap(ListView.java:623) 在android.widget.AbsListView.trackMotionScroll(AbsListView.java:2944) 在android.widget.AbsListView.onTouchEvent(AbsListView.java:2065) 在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897) 在android上的android.view.View.dispatchTouchEvent(View.java:3766) android.widget.ListView.onTouchEvent(ListView.java:3315) view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at android.view。 ViewGroup.dispatchTouchEvent(ViewGroup.java:936) at com.android.inter nal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1671) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) at android.app.Activity.dispatchTouchEvent( Activity.java:2086) at android.view.View.Root.handleMessage(ViewRoot.java:1785) at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1655) 。 os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:4627) at java.lang。方法.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) a t com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) at dalvik.system.NativeStart.main (Native Method)

+0

在什麼消息出現在logcat的 – Ayudh 2011-04-06 07:42:51

+0

ERROR/AndroidRuntime(381)崩潰的時間:java.lang中。NullPointerException – user685275 2011-04-06 08:14:10

回答

1

我看到holder = (ViewHolder) convertView.getTag();。但我沒有看到你在if (convertView == null) {...}的主體中爲convertView調用setTag()

所以,我想你忘了行:

convertView.setTag(holder); 
+0

不能相信我錯過了,非常感謝 – user685275 2011-04-06 09:22:13

0

很可能您正在獲取ArrayIndexOutOfBound異常。使用getCount()數組適配器的方法來聲明列表的大小。

+0

添加getCount,仍然是相同的問題 – user685275 2011-04-06 08:20:26