Recycler View onClick Listerner无法将Intent putExtas发送到其他活动

时间:2019-01-06 07:52:10

标签: android android-recyclerview google-cloud-firestore

我想使用Recyclerview onClick Listerner将我的recyclerview的字符串值发送到JobDetail Activity。但是当我使用putExtra并使用getStringExtrast检索意图时,它总是返回null。

这是我的RecyclerView适配器 DiscoverAdapter.java

you are not
I am also bad
world is

}

这是我的 JobDetailActivity

public class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.ViewHolder>{
public List<JobPost> jobsList;
public Context context;
private Intent jobDetailActivity;
private FirebaseAuth mAuth ;
private FirebaseUser user;

public DiscoverAdapter(Context context, List<JobPost> jobsList){
    this.context = context;
    this.jobsList = jobsList;
    mAuth = FirebaseAuth.getInstance();
    user = mAuth.getCurrentUser();
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.job_list,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.tittle_text.setText(jobsList.get(position).getTittle());
    holder.budget_text.setText(jobsList.get(position).getBudget());
    final String description = jobsList.get(position).getDescription();
    final String email = jobsList.get(position).getEmail();
    final String status = jobsList.get(position).getStatus();
    final String username = jobsList.get(position).getUsername();
    final String titlle = holder.tittle_text.getText().toString();
    final String budget = holder.budget_text.getText().toString();
    final String coba="";

    //get post id

    final String post_id = jobsList.get(position).postId;
    holder.mView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            jobDetailActivity=null;
            int post = position;
            try {
                String current_email = user.getEmail();

                FirebaseUser user = mAuth.getCurrentUser();
                if(user !=null ){
                    jobDetailActivity = new Intent(v.getContext(), JobDetailActivityLogin.class);
                }





            }
            catch(Exception e){
                jobDetailActivity = new Intent(v.getContext(), JobDetailActivity.class);
            }




            jobDetailActivity.putExtra("coba",coba);
            jobDetailActivity.putExtra("postId",post_id);
            jobDetailActivity.putExtra("username",username);
            jobDetailActivity.putExtra("description",description);
            jobDetailActivity.putExtra("tittle",titlle);
            jobDetailActivity.putExtra("email",email);
            jobDetailActivity.putExtra("budget",budget);
            jobDetailActivity.putExtra("status",status);
            v.getContext().startActivity(jobDetailActivity);
            Toast.makeText(v.getContext(), "CLICKED " + post_id + ", " + description + ", " + titlle + ", " + email + ", " + budget+","+status, Toast.LENGTH_SHORT).show();


        }
    });




}

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

public class ViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public TextView tittle_text;
    public TextView budget_text;
    /**
    public TextView description_text,email_text, status_text,username_text;
    **/

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        tittle_text = (TextView)mView.findViewById(R.id.tittle_text);
        budget_text = (TextView)mView.findViewById(R.id.budget_text);
    }
}

}

这个myError:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.kodingsquad.lancerlots / com.example.kodingsquad.lancerlots.JobDetailActivity}:java.lang.NullPointerException:尝试调用虚拟方法'java.lang。空对象引用上的字符串android.content.Intent.getStringExtra(java.lang.String)'

1 个答案:

答案 0 :(得分:1)

代替此

tittle= intent.getStringExtra("tittle");

使用此

tittle= getIntent().getStringExtra("tittle");

您可以在上面private Intent intent =getIntent();的onCreate()上使用,因为意图不存在,并且在那里没有null