为微调器

时间:2015-06-03 18:03:56

标签: android spinner android-spinner

我知道这可能是一个相对论的简单问题,但我似乎忽略了答案。

在我的应用程序中,我有一个微调器供用户选择他们所处的状态。目前,应用程序将专注于本地受众,所以我想将默认选择设置为他们居住的状态英寸

我尝试过:

registration_screen.xml

<Spinner
    android:id="@+id/spinHomeState"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="1"
    android:entries="@array/state_list"
    android:prompt="@string/state_prompt"
    android:spinnerMode="dialog"/>

RegistrationScreen.java

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

    Spinner HomeStateSpinner = (Spinner) findViewById(R.id.spinHomeState);
    HomeStateSpinner.setSelection('9');
}

但是应用程序崩溃了以下logcat日志:

  

06-03 13:52:44.512
  1736-1736 / com.name.of.app E / AndroidRuntime:   致命异议:主要       过程:com.hollywoodcert.soupserver.hollywoodcert,PID:1736       java.lang.IndexOutOfBoundsException:索引57无效,大小为50               at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)               at java.util.Arrays $ ArrayList.get(Arrays.java:66)               在android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)               在android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:390)               在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)               在android.widget.Spinner.makeView(Spinner.java:592)               在android.widget.Spinner.layout(Spinner.java:540)               在android.widget.Spinner.onLayout(Spinner.java:502)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)               在android.widget.LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1660)               在android.widget.TableRow.onLayout(TableRow.java:123)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)               在android.widget.TableLayout.onLayout(TableLayout.java:448)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)               在android.widget.ScrollView.onLayout(ScrollView.java:1468)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)               在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)               在android.widget.LinearLayout.onLayout(LinearLayout.java:1434)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)               在android.widget.FrameLayout.onLayout(FrameLayout.java:388)               在android.view.View.layout(View.java:14817)               在android.view.ViewGroup.layout(ViewGroup.java:4631)               在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)               在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)               在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)               在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5670)               在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:761)               在android.view.Choreographer.doCallbacks(Choreographer.java:574)               在android.view.Choreographer.doFrame(Choreographer.java:544)               在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:747)               在android.os.Handler.handleCallback(Handler.java:733)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:136)               在android.app.ActivityThread.main(ActivityThread.java:5017)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)               在dalvik.system.NativeStart.main(本地方法)

我很感激任何建议。

2 个答案:

答案 0 :(得分:1)

根据Android文档,Spinner方法setSelection会收到intintboolean个参数。

在您的代码中,您的调用HomeStateSpinner.setSelection('9');传递char作为参数。另一件事是java.lang.IndexOutOfBoundsException抛出意味着你试图访问微调器上数组外的位置。

我首先尝试调用方法:

HomeStateSpinner.setSelection(9);

如果这不起作用,请尝试使用较低的值来调用它,如:

HomeStateSpinner.setSelection(2);

答案 1 :(得分:0)

使用此:

String compareValue = "9";

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_layout, list);
        dataAdapter.setDropDownViewResource(R.layout.spinner_layout);
        rooms.setAdapter(dataAdapter);
        if (!compareValue.equals(null)) {
            int spinnerPostion = dataAdapter.getPosition(compareValue);
            HomeStateSpinner.setSelection(spinnerPostion);
            spinnerPostion = 0;
        }

如果9是字符串使用上面的解决方案,否则9是你想要设置选择的索引/位置然后只使用:

HomeStateSpinner.setSelection(9);

这必须解决你的问题。