• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Unable to set the array Adapter

  • Thread starter Thread starter Paul2001
  • Start date Start date
P

Paul2001

Guest
I want to implement a list view in my android application, even though the application is build without any errors, the emulator is constantly crashing with this code. After running the debugger I saw that the variable listview = findViewById(myPlantList); is set to null. Thus, I think that the problem is that the view with the ID "myPlantList" is not findable. ALthough the id is set correctly.
```
String names [] = {"Tomatoe", "Pepper Plant", "Big Tomatoe", "Small Tomatoe"};
ListView listview = findViewById(R.id.myPlantList);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names);
listview.setAdapter(arrayAdapter);
```
Does anyone know how I should correctly find the ListView with the id myPlantList ?
 
Hi Paul.
I suggest you to declare your ListView list outside the onCreate() method, and then assign the reference to the view inside the onCreate() method.
In this mode, after the assignement (list = findViewById(R.id.myListViewData)
you must see the variable list not-null, but it must conatins the reference to the view control.
Hope this help.

Mark
 
Back
Top Bottom