2016-12-28 1 views
-2

onClickcardview 버튼에 표시하려고하는데이 오류가 발생합니다. 코드입니다 :java.lang.NullPointerException on CardView 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(); 
      } 
     }); 



    } 

로그 캣 ::

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

여기에 xml을 입력하십시오 –

+0

레이아웃에 버튼 ID "addHune"이 없습니다. – Thinsky

+1

사용중인 버튼의 90 %가 Java 코드 –

답변

0

귀하의 버튼은 null입니다. 레이아웃의 버튼 ID가 R.id.addHune입니까? 레이아웃 xml에서 확인하십시오.

+0

에서 참조하는 글꼴과 다른 레이아웃에 있습니다. 여기서 xml은 무엇입니까? 나는 그것을 보지 않는다. Xml은 다음과 같습니다. https://developer.android.com/guide/topics/ui/declaring-layout.html#write –

+0

버튼이 포함 된 XML 코드를 추가했습니다. –

+0

이 레이아웃을 사용 하시겠습니까? 보여줘 방법을 만든다. –