0

Im new in programming and im trying to make a basic application in adnroid studio. Im trying to build a spinner and I am basing my code from this "https://developer.android.com/guide/topics/ui/controls/spinner".

Now when I try to create a spinner my application crashes. I've tried everything I could find on google but nothing seems to work.

This is my code:

MainActivity.java

Spinner spinner = findViewById(R.id.workout_spinner);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this,
            R.array.workout_array, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter1);

Stings.xml:

<string-array name="workout_array">
    <item>Dumbbell Press</item>
    <item>Cable Row</item>
    <item>Shoulder Press</item>
    <item>Lat Pulldown</item>
    <item>Lateral Raise</item>
    <item>Leg Press</item>
    <item>Lunges</item>
    <item>Biceps</item>
    <item>Triceps</item>
</string-array>

and here is the fragment_slideshow.xml

<Spinner
    android:id="@+id/workout_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

And here is the error android studio gives:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.schema_app, PID: 24319
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
    at com.example.schema_app.MainActivity.onCreateOptionsMenu(MainActivity.java:70)
    at android.app.Activity.onCreatePanelMenu(Activity.java:4206)
    at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:324)
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:3070)
    at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
    at androidx.appcompat.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:456)
    at androidx.appcompat.app.ToolbarActionBar$1.run(ToolbarActionBar.java:57)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I/Process: Sending signal. PID: 24319 SIG: 9

Any help would be appreciated.

novaux
  • 3
  • 1

1 Answers1

0

As I understand from your question, your Spinner is inside fragment_slideshow which implies it is a Fragment. You are initializing the spinner inside MainActivity in which the spinner does not exist. So move your codes to your FragmentSlideshow.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_slideshow , group, false);

        Spinner spinner = (Spinner) rootView.findViewById(R.id.workout_spinner);
        ArrayAdapter<CharSequence> adapter1 =
        ArrayAdapter.createFromResource(this,
                                        R.array.workout_array,
                                        android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter1);
        return rootView;

    }
private static
  • 745
  • 8
  • 17