1

我有一个通过 Web 服务调用加载的微调器,它工作正常,但是当我滚动它或第二次选择项目时,它会使应用程序崩溃。第一次点击它工作正常。我知道这可能是数组长度的问题,但无法解决。

这是我的代码:

Log.d("ArrayAdapter",""+ SpinnerData.length);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(abc.this,android.R.layout.simple_list_item_1, SpinnerData);

spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
lv.setAdapter(spinnerArrayAdapter);

SpinnerData 是包含来自服务的数据的字符串数组。

logcat 看起来像这样:

02-15 10:40:40.350: W/System.err(992): java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
02-15 10:40:40.360: W/System.err(992):  at java.util.Vector.elementAt(Vector.java:329)
02-15 10:40:40.360: W/System.err(992):  at org.ksoap2.serialization.SoapObject.getProperty(SoapObject.java:116)
02-15 10:40:40.360: W/System.err(992):  at ums.lovely.university.StudentPracMarsksDetails.soap(StudentPracMarsksDetails.java:216)
02-15 10:40:40.360: W/System.err(992):  at ums.lovely.university.StudentPracMarsksDetails$Search$1.run(StudentPracMarsksDetails.java:322)
02-15 10:40:40.360: W/System.err(992):  at android.os.Handler.handleCallback(Handler.java:587)
02-15 10:40:40.360: W/System.err(992):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 10:40:40.360: W/System.err(992):  at android.os.Looper.loop(Looper.java:123)
02-15 10:40:40.370: W/System.err(992):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 10:40:40.370: W/System.err(992):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 10:40:40.370: W/System.err(992):  at java.lang.reflect.Method.invoke(Method.java:521)
02-15 10:40:40.370: W/System.err(992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 10:40:40.370: W/System.err(992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 10:40:40.370: W/System.err(992):  at dalvik.system.NativeStart.main(Native Method)
02-15 10:40:40.370: D/The students3rfrfg4ry(992): ghsksrrsio
02-15 10:40:40.370: D/Else The studenty(992): ghskssio
02-15 10:40:41.130: D/AndroidRuntime(992): Shutting down VM
02-15 10:40:41.130: W/dalvikvm(992): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-15 10:40:41.150: E/AndroidRuntime(992): FATAL EXCEPTION: main
02-15 10:40:41.150: E/AndroidRuntime(992): java.lang.NullPointerException
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ListView.makeAndAddView(ListView.java:1727)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ListView.fillDown(ListView.java:652)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ListView.fillSpecific(ListView.java:1284)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.ListView.layoutChildren(ListView.java:1570)
02-15 10:40:41.150: E/AndroidRuntime(992):  at android.widget.AbsListView.onLayout(AbsListView.java:1147)
4

1 回答 1

0

onSelectedItemListener 中的函数如何?
需要记住数组长度和数组索引。
Array : 0 ... x
且长度为 x + 1

如果仍然错误,尝试将 android.R.layout.simple_list_item_1 更改为 android.R.layout.simple_spinner_item

于 2013-02-15T07:04:52.233 回答