它显示Null对象错误。 当我在卡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();
}
});
我想在Button上执行Action。 哪个位于Cards RecylerView
答案 0 :(得分:0)
您是否在按钮上设置了标签?
您可以拨打.setTag()
或xml android:tag=""
。
此外,您不应在视图持有者的构造函数中设置单击侦听器,请在onBindViewHolder
中执行此操作。
答案 1 :(得分:0)
在适配器的setTag()
方法中使用onBindViewHolder
:
@Override
public void onBindViewHolder(myViewHolder viewHolder, int position) {
viewHolder.mCardView.setTag(position);
}
mCardView
类中定义了myViewHolder
。
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();
}