I have code that ought to make my ListView
clickable and bring my user to another activity. But I'm getting an error saying
Attempt to invoke virtual method 'void android.widget.ListView.setClickable(boolean)' on a null object reference
But I've no idea how it's a null object reference if the ListView
is working. Here is my feed code:
public void populateList() {
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference allBooksRef = rootRef.child("All_Books");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Book> bookModelList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Book book = ds.getValue(Book.class);
bookModelList.add(book);
}
ListView list = (ListView) findViewById(R.id.listView);
CustomListAdapter adapter = new CustomListAdapter(AdminFeed.this, bookModelList);
list.setAdapter(adapter);
list.setClickable(true);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
allBooksRef.addListenerForSingleValueEvent(valueEventListener);
list.setClickable(true);
list.setOnItemClickListener((AdapterView.OnItemClickListener) this);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(AdminFeed.this, BookIndexed.class);
i.putExtra("ValueKey", modelList.get(position).getTitle());
i.putExtra("ValueKey2", modelList.get(position).getAuthor());
startActivity(i);
title = bookModelList.get(position).getTitle();
}
Why is this throwing up such an error?