2016-12-28 5 views
-2

onClickcardviewボタンに表示しようとしていますが、このエラーが発生しています。コードは::私のXMLコードCardViewでnull例外が発生しました。OnClick

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)  

であるようなものです::

<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> 
+0

ここにあなたのxmlを入れてください –

+0

あなたのレイアウトにはボタンID "addHune"はありません。 – Thinsky

+1

あなたが使用しているボタンの90%が、Javaコード –

答えて

0

あなたのボタンはnullです。あなたのレイアウトのボタンIDはR.id.addHune?ですか?あなたのレイアウトXMLでそれを確認してください。

+0

で参照しているレイアウトとは異なるレイアウトになっています。私はそれを見ない。 Xmlは次のようなものです:https://developer.android.com/guide/topics/ui/declaring-layout.html#write –

+0

ボタンを含むXMLコードを追加しました。 –

+0

このレイアウトを使用してもよろしいですか?私を表示onメソッドを作成する –

関連する問題