0

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

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)

关于如何解决这个问题的任何建议?

谢谢。

4

2 回答 2

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

于 2013-02-02T10:13:14.607 回答
0

这是我出错的地方:

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

它应该是:

array = new String[5];
于 2013-02-02T10:19:59.747 回答