Spinner滚动崩溃的应用程序

时间:2013-02-15 06:29:39

标签: android spinner

我有一个微调器,它通过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是包含来自服务的数据的String数组。

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)

1 个答案:

答案 0 :(得分:0)

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

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