android Spinner setSelection崩溃

时间:2017-07-30 15:58:27

标签: android spinner android-spinner

所以我试图设置我的微调器的值,因此用户看到的是当前值而不是总是显示1,但它似乎总是崩溃。我尝试了各种各样的解决方案,但似乎总是因为某种原因而崩溃。请提出任何建议,我不知所措。

这是我的logcat:

java.lang.ArrayIndexOutOfBoundsException: length=35; index=35
                                                                      at 
java.util.Arrays$ArrayList.get(Arrays.java:3854)
                                                                      at 
android.widget.ArrayAdapter.getItem(ArrayAdapter.java:349)
                                                                      at 
android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:405)
                                                                      at 
android.widget.ArrayAdapter.getView(ArrayAdapter.java:371)
                                                                      at 
android.widget.Spinner.makeView(Spinner.java:800)
                                                                      at 
android.widget.Spinner.layout(Spinner.java:748)
                                                                      at 
android.widget.Spinner.onLayout(Spinner.java:710)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      atandroid.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at 
android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                      at 
android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at 
android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                      at 
android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at 
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                      at 
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                      at 
android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at 
android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                      at 
android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                      at 
com.android.internal.policy.DecorView.onLayout(DecorView.java:887)
                                                                      at 
android.view.View.layout(View.java:18804)
                                                                      at 
android.view.ViewGroup.layout(ViewGroup.java:5951)
                                                                      at 
android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2655)
                                                                      at 
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2371)
                                                                      at 
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1522)
                                                                      at 
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7098)
                                                                      at 
android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
                                                                      at 
android.view.Choreographer.doCallbacks(Choreographer.java:702)
                                                                      at 
android.view.Choreographer.doFrame(Choreographer.java:638)
                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
                                                                      at 
android.os.Handler.handleCallback(Handler.java:751)
                                                                      at 
android.os.Handler.dispatchMessage(Handler.java:95)
                                                                      at 
android.os.Looper.loop(Looper.java:154)
                                                                      at 
android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                      at 
java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                      at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

这是我的代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);

maxTimeSpinner = (Spinner)findViewById(R.id.maxTime);

String[] times = new String[]{"35"};

ArrayAdapter<String> times_list = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, times);

maxTimeSpinner.setAdapter(times_list);

maxTT = "35";  // this is for testing

maxTimeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {

            maxTT = parent.getItemAtPosition(position).toString();//Double.parseDouble(sp.getString("maxTT", parent.getItemAtPosition(position).toString()));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub
        }
    });

maxTimeSpinner.setSelection(Integer.parseInt(maxTT));
}

1 个答案:

答案 0 :(得分:0)

您正在将只有一个String项的Array传递给ArrayAdapter。并调用maxTimeSpinner.setSelection(35)。尝试maxTimeSpinner.setSelection(0)。索引从0开始,而不是1.