ThatBoiJo
ThatBoiJo

Reputation: 319

Null Pointer Exception when setting spinner adapter

I made a spinner with two items, and i wanted to change the text color to white so i made an xml...

<?xml version="1.0" encoding="utf-8"?>

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white" />

I tried to do this to set the adapter....

    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_text);
    spinner.setAdapter(adapter);

But it gives me an error! It says there's a null pointer exception at the, spinner.setAdapter(), line. I've seen other questions with people doing this and it actually working so i dont know what's going on. Here's the code for the spinner....

        <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sEntries" />

The initial problem has been fixed, but now it's not showing any of the entries.

Upvotes: 2

Views: 1181

Answers (1)

Manu
Manu

Reputation: 81

Are you inflating the xml layout where you defined your spinner ? That is a very common reason for NPE. You need to inflate the layout before using resources defined in it.

Upvotes: 1

Related Questions