I am trying to create ListView
and fill it with data , but when i launch the application crashes and shows me a message
Unfortunately,…
Change:
ArrayAdapter<String> value = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.mylist, actions);
To this:
ArrayAdapter<String> value = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, actions);
//OR
ArrayAdapter<String> value = new ArrayAdapter<String>(this,
android.R.id.text1, actions);
It seems that your misused its constructor.
Try this:
ArrayAdapter<String> value = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, actions);
In ArrayAdapter Documentaion, the third parameter should be an ID of a TextView
Your constructor for the arrayadapter is incorrect.
ArrayAdapter(this,android.R.layout.simple_list_item_1, R.id.mylist, actions);
I believe R.id.mylist is the identifier for the listview? With this constructor it is actually expecting the identifier of the textview that is part of the layout android.R.layout.simple_list_item_1
It seem that your layout is out of scope of the code and thus cannot be loaded. When you check the log you can see the error "NullPointerException" which indicates that you are trying to access an object that does not exist.
Probably it is the line
ListView v = (ListView)findViewById(R.id.mylist);
that is not creating the object as you expect. If you put a breakpoint on this line you will see that is is null and by stepping to the next line
v.setTextFilterEnabled(true);
you will see that the exception occurs here.
Please check your references and how you invoke the layout in order to get the proper object loading.
You are getting NullPointerException because of your ArrayAdapter constructor , the third parameter should be the TextView inside the layout (2nd parameter) , change it to android.R.id.text1
and try again.