0

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
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • post your logcat error. – Deepak Goyal Sep 18 '15 at 22:39
  • Is the spinner null, as Deepak says post the logcat. – Lucas Crawford Sep 18 '15 at 22:41
  • Added my answer, it is probably related to not using a type for the Array Adapter and the layout type (check my modifications) – Lucas Crawford Sep 18 '15 at 22:45
  • @LucasCrawford 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 – Mika Izmylove Sep 18 '15 at 22:48
  • @LucasCrawford מנהל אחראי משמרת צ׳קר מלצר טבח – Mika Izmylove Sep 18 '15 at 22:53

4 Answers4

0

Try giving the ArrayAdapter a type:

ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this, R.array.worker, android.R.layout.simple_spinner_item);
Lucas Crawford
  • 3,078
  • 2
  • 14
  • 25
  • It still shows me a nullPointerExeption : Caused by: java.lang.NullPointerException: Attempt to get length of null array at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214) at android.content.res.Resources.getTextArray(Resources.java:503) at android.widget.ArrayAdapter.createFromResource(ArrayAdapter.java:430) at com.example.hay.myapplication.UserCreatingActivity.onCreate(UserCreatingActivity.java:45) at android.app.Activity.performCreate(Activity.java:5990) at – Mika Izmylove Sep 18 '15 at 22:44
  • I made another edit. Try that. Also link your R.array.worker – Lucas Crawford Sep 18 '15 at 22:48
  • מנהל אחראי משמרת צ׳קר מלצר טבח – Mika Izmylove Sep 18 '15 at 22:51
0

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);
Distwo
  • 11,569
  • 8
  • 42
  • 65
0

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);
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
-1

change

 attr.xml

directory

from

...\app\src\main\res\values-w820dp

to

...\app\src\main\res\values
geeekfa
  • 1,169
  • 1
  • 10
  • 15