单击Android Spinner会使应用程序崩溃

时间:2013-02-02 09:53:43

标签: android spinner android-spinner

我在我的应用中定义了一个微调器,它在主要活动开始时加载。活动成功加载,并使用来自另一个类的String数组的值加载微调器。

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);

}

点击微调器后,应用程序崩溃并显示以下跟踪:

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)

有关如何解决此问题的任何建议?

感谢。

2 个答案:

答案 0 :(得分:0)

我在你的代码中找到了什么。您的代码中找不到资源。所以你看看了。

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);

请参阅更多详情http://developer.android.com/guide/topics/ui/controls/spinner.html

答案 1 :(得分:0)

这是我出错的地方:

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

应该是:

array = new String[5];