0

こんにちは、アプリケーションの実行中にヌル ポインター例外が発生します。これが私のコードです。

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)
4

2 に答える 2

0

初めてのconvertViewときに、膨張したxmlファイルをに割り当てます。null

if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.new_item, null);
    }
于 2012-09-26T11:25:45.243 に答える
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;
    }
}
于 2012-09-26T11:28:54.643 に答える