【发布时间】:2016-09-26 06:12:04
【问题描述】:
我可以在onClick 事件中显示Toast,但无法加载新的Activity。
这是我的代码:
public class AppointmentAdapter extends RecyclerView.Adapter<AppointmentAdapter.MyViewHolder> {
private ArrayList<Appointment> appointmentList;
private Context mContext;
public AppointmentAdapter(Context context) {
mContext = context;
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Button mView, mSend;
public MyViewHolder(View view) {
super(view);
mView = (Button) view.findViewById(R.id.button_view_appointment);
mSend = (Button) view.findViewById(R.id.button_send_appointment);
mView.setOnClickListener(this);
mSend.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == mView.getId()) {
// Toast.makeText(v.getContext(), "View ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
Intent detailsIntent = new Intent(mContext, DetailsActivity.class);
mContext.startActivity(detailsIntent);
}
if (v.getId() == mSend.getId()) {
// Toast.makeText(v.getContext(), "Send ITEM PRESSED = " + String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
/................./
}
这行代码不起作用。
Intent detailsIntent = new Intent(mContext, DetailsActivity.class);
mContext.startActivity(detailsIntent);
}
当我运行这个应用程序时得到NullPointException
请帮忙。提前致谢
【问题讨论】:
-
如果您绝对确定问题出在该特定行,那么唯一可能导致
NullPointerException是mContext为空 -
NullPointException表示您正在调用不存在的东西。完全发布您的堆栈跟踪。
标签: android android-recyclerview onclicklistener