自定义适配器中的空指针异常

时间:2012-09-26 11:09:29

标签: android android-layout android-adapter

您好我在运行我的应用程序时遇到空指针异常。 这是我的代码:

ProductAdapter.java

public class ProductAdapter extends BaseAdapter {

    Context context;
    List<Product> products = new ArrayList<Product>();
    private boolean showCheckbox;


    public ProductAdapter(Context context, List<Product> products,
            boolean showCheckbox) {
        super();
        this.context = context;
        this.products = products;
        this.showCheckbox = showCheckbox;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return products.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.new_item, null);
        }

        Product myProduct = products.get(position);

        TextView title = (TextView) convertView.findViewById(R.id.txt_title);
        title.setText(myProduct.getTitle());

        TextView cost = (TextView) convertView.findViewById(R.id.txt_cost);
        cost.setText(myProduct.getCost());

        ImageView image = (ImageView) convertView.findViewById(R.id.imageView1);
        CheckBox selectBox = (CheckBox) convertView.findViewById(R.id.CheckBoxSelect1);

        if(!showCheckbox){
            selectBox.setVisibility(View.GONE);
        }
        else{
            if(myProduct.selected == true)
                selectBox.setChecked(true);
            else
                selectBox.setChecked(false);
        }

        return convertView;
    }
}

这是我的布局xml文件
new_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />



    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <TextView
        android:id="@+id/txt_cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <CheckBox
        android:id="@+id/CheckBoxSelect1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这里我得到一个nullpointer异常请帮帮我。 这是我的logcat。

09-26 16:37:23.894: D/AndroidRuntime(2611): Shutting down VM
09-26 16:37:23.894: W/dalvikvm(2611): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
09-26 16:37:23.904: E/AndroidRuntime(2611): FATAL EXCEPTION: main
09-26 16:37:23.904: E/AndroidRuntime(2611): java.lang.NullPointerException
09-26 16:37:23.904: E/AndroidRuntime(2611):     at com.shoppingcart.ProductAdapter.getView(ProductAdapter.java:59)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.ListView.fillDown(ListView.java:672)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.ListView.fillFromTop(ListView.java:732)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.ListView.layoutChildren(ListView.java:1625)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.AbsListView.onLayout(AbsListView.java:1863)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.View.layout(View.java:11158)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.View.layout(View.java:11158)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.View.layout(View.java:11158)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.View.layout(View.java:11158)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.View.layout(View.java:11158)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1462)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.os.Looper.loop(Looper.java:137)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 16:37:23.904: E/AndroidRuntime(2611):     at dalvik.system.NativeStart.main(Native Method)

谢谢。 这是我的Logcat。

09-26 16:58:35.205: D/AndroidRuntime(2749): Shutting down VM
09-26 16:58:35.205: W/dalvikvm(2749): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
09-26 16:58:35.225: E/AndroidRuntime(2749): FATAL EXCEPTION: main
09-26 16:58:35.225: E/AndroidRuntime(2749): java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageView
09-26 16:58:35.225: E/AndroidRuntime(2749):     at com.shoppingcart.ProductAdapter.getView(ProductAdapter.java:66)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.ListView.makeAndAddView(ListView.java:1772)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.ListView.fillDown(ListView.java:672)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.ListView.fillFromTop(ListView.java:732)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.ListView.layoutChildren(ListView.java:1625)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.AbsListView.onLayout(AbsListView.java:1863)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.View.layout(View.java:11158)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.View.layout(View.java:11158)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.View.layout(View.java:11158)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.View.layout(View.java:11158)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.View.layout(View.java:11158)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewGroup.layout(ViewGroup.java:4197)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1462)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2382)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.os.Looper.loop(Looper.java:137)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at android.app.ActivityThread.main(ActivityThread.java:4340)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 16:58:35.225: E/AndroidRuntime(2749):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

第一次将convertView充值的xml文件分配给null

if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.new_item, null);
    }

答案 1 :(得分:0)

 public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater.inflate(R.layout.new_item, null);
        }
  

将上述代码更改为

  public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout rowLayout = null;
            if (rowLayout == null) {
                    rowLayout = (LinearLayout) LayoutInflater.from(context)
                            .inflate(R.layout.new_item, parent, false);
                } 
 Product myProduct = products.get(position);

        TextView title = (TextView) rowLayout.findViewById(R.id.txt_title);
        title.setText(myProduct.getTitle());

        TextView cost = (TextView) rowLayout.findViewById(R.id.txt_cost);
        cost.setText(myProduct.getCost());

        ImageView image = (ImageView) rowLayout.findViewById(R.id.imageView1);
        CheckBox selectBox = (CheckBox) rowLayout.findViewById(R.id.CheckBoxSelect1);

        if(!showCheckbox){
            selectBox.setVisibility(View.GONE);
        }
        else{
            if(myProduct.selected == true)
                selectBox.setChecked(true);
            else
                selectBox.setChecked(false);
        }

        return rowLayout;
    }
}