CardView OnClick上的java.lang.NullPointerException

时间:2016-12-28 09:21:46

标签: android onclick cardview

我正在尝试在onClick按钮上显示cardview,我收到此错误消息。守则是::

 StaggeredGridLayoutManager gridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager = new GridLayoutManager(getApplicationContext(), 2);

        recyclerView = (RecyclerView)findViewById(R.id.cart_recycle);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(gridLayoutManager);

        adapter = new NewAdapter(models);

        recyclerView.setAdapter(adapter);

        name = getResources().getStringArray(R.array.item_name);
        price = getResources().getStringArray(R.array.item_cost);

        int count = 0;
        for(int image:images)
        {

            NewModel model = new NewModel(image,name[count],price[count]);
            count++;
            models.add(model);
        }
        adapter = new NewAdapter(models);
        recyclerView.setAdapter(adapter);


        button = (Button) findViewById(R.id.addHune);
       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "hello click", Toast.LENGTH_SHORT).show();
                add();
            }
        });



    }

Logcat ::

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at anish.extras.CartActivity.onCreate(CartActivity.java:78)
                      at android.app.Activity.performCreate(Activity.java:6303)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) 

我的XML代码就像::

   <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"

    >

    <RelativeLayout
        android:id="@+id/ll"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <ImageView
            android:id="@+id/ItemImage"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:src="@drawable/g"
            android:scaleType="centerCrop"

            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="5dp"
            android:id="@+id/view"
            android:elevation="20dp"
            android:layout_below="@+id/ItemImage"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Gypsum Board"
                android:padding="5dp"
                android:id="@+id/ItemName"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="$99"
                android:padding="5dp"
                android:layout_margin="5dp"
                android:layout_below="@+id/ItemName"
                android:textStyle="bold"
                android:id="@+id/ItemPrice"/>
            <Button
                android:layout_width="60dp"
                android:layout_height="40dp"
                android:textSize="10dp"
                android:layout_below="@+id/ItemName"
                android:id="@+id/addHune"
                android:layout_marginRight="10dp"
                android:layout_alignParentRight="true"
                android:textAllCaps="false"
                android:text="+ Add"/>
        </RelativeLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:0)

您的按钮为空。您确定布局中的按钮ID是R.id.addHune吗?请在布局xml上查看。