When trying to connect an adapter:

Spinner spinner = (Spinner) findViewById(R.id.input_words_spinner_1); ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.times, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); 

On the last line, the program crashes. Here is the array code:

 <string-array name="times"> <item>Один</item> <item>Два</item> </string-array> 

stacktrace errors:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dugin_rostislav.dictionary/com.dugin_rostislav.dictionary.InputData_DialogActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.dugin_rostislav.dictionary.InputData_DialogActivity.onCreate(InputData_DialogActivity.java:39) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)            at android.app.ActivityThread.access$600(ActivityThread.java:141)            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)            at android.os.Handler.dispatchMessage(Handler.java:99)            at android.os.Looper.loop(Looper.java:137)            at android.app.ActivityThread.main(ActivityThread.java:5041)            at java.lang.reflect.Method.invokeNative(Native Method)            at java.lang.reflect.Method.invoke(Method.java:511)            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)            at dalvik.system.NativeStart.main(Native Method) 

Here is the Spinner a code:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/gray" android:orientation="vertical" > <LinearLayout android:layout_width="300dp" android:layout_height="45dp" android:layout_margin="3dp" android:background="@color/white" > <Spinner android:id="@+id/input_words_spinner_1" android:layout_width="match_parent" android:layout_height="45dp" /> </LinearLayout> <LinearLayout android:id="@+id/input_words_linearLayout_1" android:layout_width="300dp" android:layout_height="wrap_content" > <FrameLayout android:id="@+id/input_words_frameLayout_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" > </FrameLayout> </LinearLayout> <Button android:id="@+id/input_words_button_1" android:layout_width="300dp" android:layout_height="40dp" android:layout_margin="3dp" android:background="@drawable/button_background_one" android:text="@string/button_2" android:textColor="@color/black" /> </LinearLayout> 

What am I doing wrong?

  • Well, this is not your first time NPE ... Your Spinner = null. - Yuriy SPb
  • @Yuriy SPb, yes, but last time I confused the variable, at first I "fluttered" everything into the v variable, and then in onClick tried to get it out, although there was already another variable there. And what's wrong here? In the layout file, this element is definitely there, why the error? - user189127
  • If it is, then the spinner should not be null. And he is null. Paradox. - Yuriy SPb
  • @Yuriy SPb, I added the code layout with Spinner . He is there. Maybe the error is different? - user189127
  • Well, maybe you in the activation markup is not the one in the setContentView () downloaded ... Xs. It looks fine. - Yuriy SPb

1 answer 1

Search for markup elements is necessary only after it is loaded, i.e. after the call

 setContentView(): 
  • And before downloading it can not? - user189127
  • Yes, no way ...... - YuriySPb