使RecyclerView中的项目可单击时出现问题

我知道有这样的问题,但我的问题无法解决。

我正在尝试将'RecyclerView'设置为'OnItemClicklistener',但是当我运行该应用程序时,它将停止。.

你能告诉我错误在哪里吗?

以下是代码:(请注意,错误在于使该项目可点击)

<chrono>

这是“ RecyclerView”的“适配器”:

public class Teacheractivity extends AppCompatactivity {
private FirebaseAuth mAuth=FirebaseAuth.getInstance();
private FirebaseUser currentUser= mAuth.getcurrentUser();;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private String UserId =currentUser.getUid();
private static String city;
private TeacherAdapter teacherAdapter;
public static final String EXTRA_PATH = "com.example.exercise.EXTRA_PATH";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teacher);


    setUpRecyclerView();


  teacherAdapter.setOnItemClicklistener(new TeacherAdapter.OnItemClicklistener() {
  @Override
  public void onItemClick(Documentsnapshot documentsnapshot,int position) {

    String path = documentsnapshot.getReference().getPath();
    Intent intent = new Intent(Teacheractivity.this,Secondactivity.class);
    intent.putExtra(EXTRA_PATH,path);
    startactivity(intent);

    }
    });

}// end of onCreate


private void setUpRecyclerView(){
    Query query =   db.collection("Teachers");

    FirestoreRecyclerOptions < Teacher > options = new 
    FirestoreRecyclerOptions.Builder<Teacher>()
                            .setQuery(query,Teacher.class)
                            .build();

    teacherAdapter = new TeacherAdapter(options);

    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);    //for performane reasons
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setadapter(teacherAdapter);
    teacherAdapter.startListening();

}

@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onStop() {
    super.onStop();
    teacherAdapter.stopListening();
}

}// end of class

logcat的一部分:

  

E / Android运行时:致命异常:主要       进程:com.example.exercise,PID:29966       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exercise / com.example.exercise.Teacheractivity}:java.lang.NullPointerException:尝试调用虚拟方法'void com.example.exercise.TeacherAdapter.setOnItemClicklistener( com.example.exercise.TeacherAdapter $ OnItemClicklistener)”上的空对象引用

zhyl1109 回答:使RecyclerView中的项目可单击时出现问题

您遇到以下错误:

  

E / AndroidRuntime:致命例外:主进程:com.example.exercise,PID:29966 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.exercise / com.example.exercise.TeacherActivity}:java .lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.example.exercise.TeacherAdapter.setOnItemClickListener(com.example.exercise.TeacherAdapter $ OnItemClickListener)'

因为此时您正在对.setOnItemClickListener()的{​​{1}}对象调用teacherAdapter。要解决此问题,您只需移动以下代码块:

null

在适配器声明之后:

teacherAdapter.setOnItemClickListener(new TeacherAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(DocumentSnapshot documentSnapshot,int position) {

        String path = documentSnapshot.getReference().getPath();
        Intent intent = new Intent(TeacherActivity.this,SecondActivity.class);
        intent.putExtra(EXTRA_PATH,path);
        startActivity(intent);

    }
});

使用您的teacherAdapter = new TeacherAdapter(options); 方法,您的问题将得到解决。

本文链接:https://www.f2er.com/3169778.html

大家都在问