开发者

IndexOutOfBounds on Spinner?

开发者 https://www.devze.com 2023-03-01 19:25 出处:网络
I have a problem with interpretation of stack trace.. The peoblem is that there is no class from my package and so it is hard for me to find out what is happening.. As I fallowed trace, I think it\'s

I have a problem with interpretation of stack trace.. The peoblem is that there is no class from my package and so it is hard for me to find out what is happening.. As I fallowed trace, I think it's about an adapter and spinner, but on that page I have 6 spinners, I debug them all and didn't find anything weird (like selectedItemPosition or something)..

Maybe someone had similar problem? Here's th trace. If you need some code, tell me

    Uncaught handler: thread main exiting due to uncaught exception
    java.lang.IndexOutOfBoundsException
     at java.util.Arrays$ArrayList.get(Arrays.java:72)
     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:298)
     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:351)
     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:198)
     at android.view.View.measure(View.java:7987)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
     at android.view.View.measure(View.java:7987)
     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:893)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
     at android.widget.ScrollView.onMeasure(ScrollView.java:276)
     at android.view.View.measure(View.java:7987)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
     at android.view.View.measure(View.java:7987)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
     at and开发者_运维问答roid.view.View.measure(View.java:7987)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
     at android.view.View.measure(View.java:7987)
     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4363)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
     at dalvik.system.NativeStart.main(Native Method)

I don't know what to do..


See if you have setSelection() called on any of the spinners that may have index that greater than the spinner's adapter size. e.g. if your spinner has two items, and if you call setSelection(2), this will result in IndexOutOfBoundsException exception.


Furthering @FrancescoR 's comment, this code may help you avoid your code crashing while you try and track down the bad call. I had to make this a reply because adding code in the comments was quite nasty.

private void spinnerSetSelectionChecked(Spinner spin, int index) {
    spinnerSetSelectionChecked(spin, index, true);
}
private void spinnerSetSelectionChecked(Spinner spin, int index, boolean animate) {
    final int count = spin.getAdapter().getCount();
    if( (count > 0) && (index < count) ) {
        spin.setSelection(index, animate);
    }
    else { // do nothing
    }
}


To find what spinner is the one with the problem:

  • Make a backup copy of your page
  • Delete 5 of the 6 spinners
  • Test the page
  • Repeat to keep only 1 spinner at the time, until you have done to test each spinner on your page

That's what I'd do. I hope this will be of help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号