第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使 RecyclerView 中的項目可點擊時出現(xiàn)問題

使 RecyclerView 中的項目可點擊時出現(xiàn)問題

搖曳的薔薇 2024-01-25 23:05:06
我知道有這樣的問題,但沒有解決我的..我正在嘗試將 'RecyclerView' 設(shè)為 'OnItemClickListener' ,但是當(dāng)我運行該應(yīng)用程序時,它停止了。你能告訴我錯誤在哪里嗎?這是代碼:(請注意錯誤在于使項目可點擊)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條經(jīng)驗 獲得超4個贊

您收到以下錯誤:

E/AndroidRuntime:致命異常:主進程:com.example.exercise,PID:29966 java.lang.RuntimeException:無法啟動活動 ComponentInfo{com.example.exercise/com.example.exercise.TeacherActivity}:java.lang。 NullPointerException:嘗試在空對象引用上調(diào)用虛擬方法“void com.example.exercise.TeacherAdapter.setOnItemClickListener(com.example.exercise.TeacherAdapter$OnItemClickListener)”

因為您正在調(diào)用.setOnItemClickListener()當(dāng)時的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()方法,你的問題就會得到解決。


查看完整回答
反對 回復(fù) 2024-01-25
  • 1 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號