问题
I made an array list in one of my value's folder, now when i want to adapt it to the spinner by retrieving it from the source folder it says NullPointerException
:
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.worker,android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
Messages in LogCat:
Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f090001 at android.content.res.Resources.getStringArray(Resources.java:527) at com.example.hay.myapplication.UserCreatingActivity.onCreate(UserCreatingActivity.java:46) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:227
回答1:
Try giving the ArrayAdapter a type:
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this, R.array.worker, android.R.layout.simple_spinner_item);
回答2:
since you have the exception:
`Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f090001 at
I see that your problem is the last value inside the createFromResource()
method:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.worker,android.R.layout.simple_list_item_1);
the last value must be the array:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,android.R.layout.simple_list_item_1,R.array.worker);
回答3:
Try to flip the two last parameters in createFromRessource
:
Replace:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.worker,android.R.layout.simple_list_item_1);
By:
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this, android.R.layout.simple_list_item_1, R.array.worker);
In your code:
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this, android.R.layout.simple_list_item_1, R.array.worker);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
回答4:
change
attr.xml
directory
from
...\app\src\main\res\values-w820dp
to
...\app\src\main\res\values
来源:https://stackoverflow.com/questions/32662048/adapter-for-a-spinner-makes-nullpointerexception