Tym
Tym

Reputation: 113

Android Studio Spinner Causing App to Crash

This is doing my crust in!

I'm new to android app programming and my JAva in minimal to say the least. Obviously spending a lot of time searching around for how to do things.

I have a spinner in one of my layouts which I'm trying to populate from a string array.

I'm basing my work on this item:

Android: Create spinner programmatically from array

It doesn't matter what I do, every time it gets to the

spinner.setAdapter(spinnerArrayAdapter); 

line it causes the app to crash.

I declare the spinner and the array adapter like this:

Spinner spinner;
ArrayAdapter<String> spinnerArrayAdapter;

I've just noticed it says that spinnerArrayAdapter is never used despite it appearing the code below.

I even copied this code in directly to test it and it still crashes at that point: EDIT - Correct code from my app here rather than that from the example page.

    // Array of choices
    String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};

    // Selection of the spinner
    Spinner spinner = (Spinner) findViewById(R.id.spinnerArrays);

    // Application of the Array to the Spinner
    spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, colors);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    spinner.setAdapter(spinnerArrayAdapter);

I'm targeting Froyo as I want this to have maximum tolerance of the users' android version - although it is a limited appeal app specific to some hardware we have built.

Question is - why the heck does it keep bailing out when trying to populate the spinner.

If you need any more info, I'm afraid you'll have to spell out exactly where I get it from as I'm totally new to Android Studio!

This is all the red stuff from Logcat...

10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime: FATAL EXCEPTION: main
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{app.ictis.com.myarraydaya/app.ictis.com.myarraydaya.LiveStatus}: java.lang.NullPointerException
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:  Caused by: java.lang.NullPointerException
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at app.ictis.com.myarraydaya.LiveStatus.onCreate(LiveStatus.java:39)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:123) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:521) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-10 12:55:10.596 18044-18044/app.ictis.com.myarraydaya E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 

My xml for the spinner is like this:

    android:id="@+id/spinnerArrays"
    android:layout_width="match_parent"
    android:layout_alignParentRight="true"
    android:layout_above="@+id/buttonCloseLive"
    android:layout_height="wrap_content">

EDIT: The full onCreate is as follows:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setupCloseButton();
    populateSpinner();
    // Application of the Array to the Spinner
    spinner = (Spinner) findViewById(R.id.spinnerArrays);

    // Array of choices
    String colors[] = {"Red","Blue","White","Yellow","Black", "Green","Purple","Orange","Grey"};

    // Selection of the spinner
    Spinner spinner = (Spinner) findViewById(R.id.spinnerArrays);

    // Application of the Array to the Spinner
    spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, colors);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    spinner.setAdapter(spinnerArrayAdapter);



   checkStatus();
}

If I comment out the

    spinner.setAdapter(spinnerArrayAdapter);

Line, it works...

The populateSpinner() method doesn't actually do anything yet apart from println the contents of a dynamically created string array. CheckStatus() has no bearing on this either, and works fine. To test this I have commented out both lines and it still crashes.

Upvotes: 1

Views: 4167

Answers (2)

user8478561
user8478561

Reputation:

I had the same issue as stated below which was displayed in the Logcat section

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

This is because your id was misssing oncreate

I removed my spinner code from the onCreate method and pasted it in the onCreateOptions Menu as below:

  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    Spinner spinner = findViewById(R.id.partNumberRevisionSpinner);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, 
     R.array.wos, android.R.layout.simple_spinner_item);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter1);
    return true;
}

My array in string.xml array

  <string-array name="wos">
    <item>6021665</item>
    <item>6021666</item>
    <item>6021667</item>
    <item>6021668</item>
    <item>6021669</item>
    <item>6021671</item>
    <item>6021672</item>
    <item>6021673</item>
</string-array>

MY spinner in XML

   <Spinner
        android:id="@+id/partNumberRevisionSpinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true" />

This worked for me now my spinner loads in my Navigation Drawer with no issues.

Upvotes: 0

bendaf
bendaf

Reputation: 3031

I thing the problem is with the id of your spinner, in your code it's R.id.myspinner and in the xml it's R.id.spinnerArrays.

It should be the same, so try to change your code to Spinner spinner = (Spinner) findViewById(R.id.spinnerArrays); in your code.

EDIT:

You also miss the setContentView(R.layout.activity_layout); from your activity. This line is responsible for saying where to search for the veiws (for example your spinner) of your activity. It should be the second line after super.onCreate()!;

Upvotes: 1

Related Questions