当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Spinner on click crashes the application

问题描述:

I have defined a spinner in my app that loads on start of the main activity. The activity loads successfully and also loads the spinner with the values from a String array from another class.

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Spinner spinner = (Spinner) findViewById(R.id.spinner);

AnotherClass ac = new AnotherClass();

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item, ac.array);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(adapter);

}

On clicking on the spinner the app crashes with the following trace:

02-02 15:14:15.947: E/AndroidRuntime(1619): FATAL EXCEPTION: main

02-02 15:14:15.947: E/AndroidRuntime(1619): java.lang.NullPointerException

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:415)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:569)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:565)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.Spinner.measureContentWidth(Spinner.java:513)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.Spinner$DropdownPopup.show(Spinner.java:739)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.widget.Spinner.performClick(Spinner.java:453)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.view.View$PerformClick.run(View.java:14105)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.os.Handler.handleCallback(Handler.java:605)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.os.Handler.dispatchMessage(Handler.java:92)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.os.Looper.loop(Looper.java:137)

02-02 15:14:15.947: E/AndroidRuntime(1619): at android.app.ActivityThread.main(ActivityThread.java:4424)

02-02 15:14:15.947: E/AndroidRuntime(1619): at java.lang.reflect.Method.invokeNative(Native Method)

02-02 15:14:15.947: E/AndroidRuntime(1619): at java.lang.reflect.Method.invoke(Method.java:511)

02-02 15:14:15.947: E/AndroidRuntime(1619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

02-02 15:14:15.947: E/AndroidRuntime(1619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

02-02 15:14:15.947: E/AndroidRuntime(1619): at dalvik.system.NativeStart.main(Native Method)

Any suggestions on how I can fix this?

Thanks.

网友答案:

what i am finding in your code. there is resource not found in your code. so you have check it out.

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

please refer more details http://developer.android.com/guide/topics/ui/controls/spinner.html

网友答案:

This is where I was going wrong:

array = new String[12];
array[0] = "JanuaryX";
array[1] = "FebruaryX";
array[2] = "March";
array[3] = "April";
array[4] = "May";

It should have been:

array = new String[5];
分享给朋友:
您可能感兴趣的文章:
随机阅读: