我有一个活动,其中有2个微调器。我给旋转器填充了一个xml文件中的列表,如下所示:
<outil id="1" name="Décompacteur">
<travail id="1">Travail au sol</travail>
<travail id="1001">Autre</travail>
</outil>
<outil id="2" name="Cultivateur">
<travail id="3">Travail au sol</travail>
<travail id="1002">Autre</travail>
</outil>
在另一项活动中,我可以更改构建列表的文件。
我的问题是:我在一长串工具的末尾选择了一个工具(outil),例如position = 6.然后我更改了文件,工具列表现在只有4个项目。我设置了一个新的适配器:
listNameOutil = getOutilNameList(listOutilObject);
adapterOutil = new ArrayAdapter<String>(this, R.layout.spinners_base, listNameOutil);
adapterOutil.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_outil.setAdapter(adapterOutil);
但是我仍然会收到错误,如果堆栈跟踪中没有“由...引起”,我不知道它来自哪里...请注意我也做spinner_outil.setSelection(positionSaved);
。起初,我试图在这一行上捕获异常,但似乎不是这一行是问题所在。
有什么想法可以解决这个问题吗?
这是堆栈跟踪:
06-27 14:46:01.811: W/dalvikvm(20824): threadid=1: thread exiting with uncaught exception (group=0x40d0c930)
06-27 14:46:01.821: E/AndroidRuntime(20824): FATAL EXCEPTION: main
06-27 14:46:01.821: E/AndroidRuntime(20824): java.lang.IndexOutOfBoundsException: Invalid index 6, size is 4
06-27 14:46:01.821: E/AndroidRuntime(20824): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
06-27 14:46:01.821: E/AndroidRuntime(20824): at java.util.ArrayList.get(ArrayList.java:304)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.makeAndAddView(Spinner.java:546)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.layout(Spinner.java:495)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.Spinner.onLayout(Spinner.java:459)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.View.layout(View.java:14008)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewGroup.layout(ViewGroup.java:4373)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer.doFrame(Choreographer.java:532)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Handler.handleCallback(Handler.java:725)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Handler.dispatchMessage(Handler.java:92)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.os.Looper.loop(Looper.java:137)
06-27 14:46:01.821: E/AndroidRuntime(20824): at android.app.ActivityThread.main(ActivityThread.java:5041)
06-27 14:46:01.821: E/AndroidRuntime(20824): at java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:46:01.821: E/AndroidRuntime(20824): at java.lang.reflect.Method.invoke(Method.java:511)
06-27 14:46:01.821: E/AndroidRuntime(20824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-27 14:46:01.821: E/AndroidRuntime(20824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-27 14:46:01.821: E/AndroidRuntime(20824): at dalvik.system.NativeStart.main(Native Method)
编辑:
在开始时,我绑定到我的服务。然后,在onServiceConnected中,我向服务询问配置信息:
try {
remoteServiceMain.requeteLireConfig();
} catch (RemoteException e) {
e.printStackTrace();
}
然后,仍然在onServiceConnected中,我定义了布局:
spinner_outil = (Spinner) findViewById(R.id.spinner_outil_layout);
spinner_outil.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
}
}
然后onServiceConnected完成。请注意,我在错误之前得到了最后一个日志。
REDDIT:
我在setSelection(positionSaved);
之后spinner_outil.setAdapter(adapterOutil);
,并在调用后从服务中得到答案时设置了positionSaved:
try {
remoteServiceMain.requeteLireConfig();
} catch (RemoteException e) {
e.printStackTrace();
}
服务发送 positionSaved
,它从配置文件中获取它。用户点击微调器的项目时会保存positionSaved
。我从服务而不是直接从文件中获取它的原因是我对其他东西使用相同的方法