0

错误日志:

06-28 12:07:38.227: E/AndroidRuntime(1199): FATAL EXCEPTION: main
06-28 12:07:38.227: E/AndroidRuntime(1199): java.lang.NullPointerException
06-28 12:07:38.227: E/AndroidRuntime(1199):     at com.innoventes.cpea.view.DashboardAdapter.getView(DashboardAdapter.java:55)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.AbsListView.obtainView(AbsListView.java:1409)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.ListView.fillSpecific(ListView.java:1290)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.ListView.layoutChildren(ListView.java:1576)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.View.layout(View.java:7175)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.View.layout(View.java:7175)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.View.layout(View.java:7175)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.View.layout(View.java:7175)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.os.Looper.loop(Looper.java:130)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at android.app.ActivityThread.main(ActivityThread.java:3683)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at java.lang.reflect.Method.invoke(Method.java:507)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-28 12:07:38.227: E/AndroidRuntime(1199):     at dalvik.system.NativeStart.main(Native Method)

我的适配器:

public class DashboardAdapter extends ArrayAdapter<ProductModel>{

Context context;
List<ProductModel> item;

public DashboardAdapter(Context context, int resourceId,List<ProductModel> items) 
{
    super(context, resourceId, items);
    this.context = context;
    this.item = items;
}
/*private view holder class*/
private class ViewHolder 
{
    ImageButton rightCaret;
    TextView txtTitle;   
} 
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder = new ViewHolder();
    ProductModel rowItem = getItem(position); 
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
    {
        convertView = mInflater.inflate(R.layout.dashboard_item,parent,false);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.dashboardItem_description);   
        holder.rightCaret = (ImageButton) convertView.findViewById(R.id.rightCaretDash);            
        holder.rightCaret.setTag(position); 
        holder.txtTitle.setText(rowItem.getProductTitle()); 
        convertView.setTag(holder);
        holder.rightCaret.setClickable(true);
        holder.rightCaret.setId(position);
        DashboardAdapter.this.notifyDataSetChanged();            
    }
    holder.rightCaret.setOnClickListener(new View.OnClickListener() 
    {            
    public void onClick(final View view) 
       {                 
            Integer index = (Integer) view.getTag(); 
            String header = item.get(index).toString();
            Intent intent = new Intent(context,ListProduct.class);
            intent.putExtra("value",header);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            notifyDataSetChanged();                                      
       }

    });

    convertView.setClickable(true);
    return convertView;
}

}

当我调试时,rightCaret imageButton 为空。请帮助我不知道如何走得更远。

4

1 回答 1

0
 if (convertView == null)  {
   //your code..
   convertView.setTag(holder);
} else 
    holder = (ViewHolder) convertView.getTag();

你创建一个新的ViewHolder随时getView()被调用,但它的字段没有被初始化。

于 2013-06-28T07:19:22.273 回答