为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

摇曳的蔷薇 2024-01-25 23:05:06
我知道有这样的问题,但没有解决我的..我正在尝试将 '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";@Overrideprotected 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 onCreateprivate 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();}@Overrideprotected void onStart() {    super.onStart();}@Overrideprotected void onStop() {    super.onStop();    teacherAdapter.stopListening();}}// end of class
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

您收到以下错误:

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() {

    @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);

按照你的setUpRecyclerView()方法,你的问题就会得到解决。


查看完整回答
反对 回复 2024-01-25
  • 1 回答
  • 0 关注
  • 88 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信