-2

我正在尝试显示onClickatcardview按钮,但出现此错误。代码是::

 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();
            }
        });



    }

日志猫 ::

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

1 回答 1

0

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

于 2016-12-28T09:25:40.927 回答