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

时间:2019-11-03 23:22:39

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

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

我正在尝试将'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)”上的空对象引用

1 个答案:

答案 0 :(得分: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()的{​​{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); 方法,您的问题将得到解决。