views:

214

answers:

2

Hello,

In my app I'm developing, I have a spinner in an alert dialog box.

The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    newFileType.setAdapter(adapter);

What am I doing wrong? :(

These Android spinners seem to be a bit complicated aswell, I don't think I'll be able to remember how to make them without referencing to the Android docs lol.

A: 

Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?

Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);

or similar? If you're trying to set its adapter before initializing it, that would explain it.

Blumer
Here's what I have:final Spinner newFileType = (Spinner) findViewById(R.id.newfiletype);
AlexPriceAP
K, that's not it then. I'd agree with Mayra: we'll need some logcat goodness.
Blumer
Here's the main error: 09-02 10:15:17.457: ERROR/AndroidRuntime(5095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexprice.webedit/com.alexprice.webedit.WebEdit}: java.lang.NullPointerException
AlexPriceAP
K, and then down somewhere beneath that, there should be another error that starts with "Caused by NullPointerException" and give you the line number where the actual null pointer exception occurs.
Blumer
Here's the "Caused by" line and the rest of the error: 09-02 10:15:17.457: ERROR/AndroidRuntime(5095): Caused by: java.lang.NullPointerException09-02 10:15:17.457: ERROR/AndroidRuntime(5095): at com.alexprice.webedit.WebEdit.onCreate(WebEdit.java:46)09-02 10:15:17.457: ERROR/AndroidRuntime(5095): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)09-02 10:15:17.457: ERROR/AndroidRuntime(5095): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)09-02 10:15:17.457: ERROR/AndroidRuntime(5095): ... 11 more
AlexPriceAP
Okay, line 46 is your problem then. Which one is that?
Blumer
There's nothing on line 46, just a space seperating a button onClickListener (Line 47) from the following code on line 45:newFileType.setAdapter(adapter);
AlexPriceAP
Hmm, I have the XML for the Spinner in a seperate XML file to main.xml, maybe that's why it is crashing my app. Is there a way around this?
AlexPriceAP
A: 

Problem solved.

I realised that the following line:

final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);

I had to change to:

final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);

With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:

final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);

But thanks for the help!

AlexPriceAP