在setOnItemClickListener RecyclerView中调用intent时出错

时间:2016-11-24 06:04:27

标签: java android

我试图将Upcoming.java用于MeetingDetails.java。我已经尝试在OnItemClickListener中干杯并且它可以工作,但是当我尝试意图时出现错误。

Upcoming.Java

mAdapter = new MeetingsAdapter(meetingList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new MeetingDivider(this, LinearLayoutManager.VERTICAL));
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

    MeetingListClickSupport.addTo(recyclerView).setOnItemClickListener(new MeetingListClickSupport.OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            Meeting meeting = meetingList.get(position);
            Intent i = new Intent(getApplicationContext() , MeetingDetails.class);
            i.putExtra("id", meeting.getId());
            i.putExtra("status", "U");
            startActivity(i);
        }
    });

MeetingListClickSupport.java

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
            mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
        }
     }

MeetingsAdapter.java

private List<Meeting> meetingsList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView mName, description;

    public MyViewHolder(View view) {
        super(view);
        mName = (TextView) view.findViewById(R.id.mName);
        description = (TextView) view.findViewById(R.id.description);
    }
}

public MeetingsAdapter(List<Meeting> meetingsList) {
    this.meetingsList = meetingsList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_meeting_list, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Meeting meeting = meetingsList.get(position);
    holder.mName.setText(meeting.getName());
    holder.description.setText(meeting.getDescription());
}

@Override
public int getItemCount() {
    return meetingsList.size();
}

我收到了这样的错误:

FATAL EXCEPTION: main
Process: com.example.erice.attendancecontroller, PID: 1358
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
                  at com.example.erice.attendancecontroller.Upcoming$1.onItemClicked(Upcoming.java:65)
                  at com.example.erice.attendancecontroller.MeetingListClickSupport$1.onClick(MeetingListClickSupport.java:20)

如果有人可以帮助我,非常感谢

1 个答案:

答案 0 :(得分:0)

在你的这一行

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());

您的传递getApplicationContext(),您必须通过Activity context

使用此

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);

然后在RecycleView.Adapter类Create Constructor。

public MeetingsAdapter : RecyclerView.Adapter
{
    Activity context;

     public MeetingsAdapter(Activity context)
        {
            this.context = context;
        }
}

现在你的OnClick使用这种方式

Intent i = new Intent(context , MeetingDetails.class);