2017-03-24 54 views
0

它顯示一個空對象錯誤。 當我取卡RecylerView我們如何獲取卡片中的Onclick按鈕值recylerView

Process: com.softedge.visioneering.tfd, PID: 9884 
        java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference 
         at com.softedge.visioneering.tfd.adapters.MyOrdersAdapter$MyOrdersViewHolder$1.onClick(MyOrdersAdapter.java:138) 
         at android.view.View.performClick(View.java:5609) 

This Is My Adapter and Button Code: 

public class MyOrdersViewHolder extends RecyclerView.ViewHolder { 

     public TextView txtOrderDescription, txtOrderQuantity, txtOrderStatus, txtOrderDate, txtExpectedDate, txtCreatedBy,txtOrderStatus_Per; 
String check; 
     public Button update_btwn; 
     public MyOrdersViewHolder(View convertView) { 
      super(convertView); 
      update_btwn=(Button) convertView.findViewById(R.id.status_update_button); 

      final FragmentManager fragmentManager = ((FragmentActivity) context).getSupportFragmentManager(); 

      update_btwn.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        id = (Integer) update_btwn.getTag(); 

        fragmentManager.beginTransaction().replace(R.id.container, new Status_update_Fragment().newInstance()).addToBackStack(" ").commit(); 
        //((FragmentActivity) context).getSupportFragmentManager().beginTransaction(). 
        // remove(((FragmentActivity) context).getSupportFragmentManager().findFragmentById(R.id.container)).commit(); 

       } 

      }); 

的按鈕值我想執行的按鈕的動作。 卡中的哪個位置RecylerView

+1

您需要setTag權得到它之前.. – Srihari

+0

我們是怎麼做的請告訴我 –

+0

我只是想知道哪個按鈕卡在卡recylerView –

回答

0
  1. 使用setTag()在適配器的onBindViewHolder方法:

    @Override 
    public void onBindViewHolder(myViewHolder viewHolder, int position) { 
        viewHolder.mCardView.setTag(position); 
    } 
    

其中mCardViewmyViewHolder類中定義。

private class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 
      public View mCardView; 

      public myViewHolder(View view) { 
       super(view); 
       mCardView = (CardView) view.findViewById(R.id.card_view); 

       mCardView.setOnClickListener(this); 
      } 
     } 
  • 使用getTag()OnClickListener實現:

    @Override 
    public void onClick(View view) { 
         int position = (int) view.getTag();   
    
        //display toast with position of cardview in recyclerview list upon click 
        Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show(); 
    } 
    
  • 0

    您是否在按鈕上設置了標籤?

    您可以致電.setTag()或xml android:tag=""

    此外,您不應該在視圖持有者的構造函數中設置點擊偵聽器,請在onBindViewHolder中執行此操作。