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

我知道有这样的问题,但没有解决我的..

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

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

这是代码:(请注意错误在于使项目可点击)


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


摇曳的蔷薇
浏览 109回答 1
1回答

慕莱坞森

您收到以下错误: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()当时的teacherAdapter对象。null要解决这个问题,您只需移动以下代码块:teacherAdapter.setOnItemClickListener(new TeacherAdapter.OnItemClickListener() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onItemClick(DocumentSnapshot documentSnapshot, int position) {&nbsp; &nbsp; &nbsp; &nbsp; String path = documentSnapshot.getReference().getPath();&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(TeacherActivity.this, SecondActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; intent.putExtra(EXTRA_PATH, path);&nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; }});在声明适配器之后:teacherAdapter = new TeacherAdapter(options);按照你的setUpRecyclerView()方法,你的问题就会得到解决。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java