【问题标题】:Android Array out of Bounds ExceptionAndroid 数组越界异常
【发布时间】:2014-03-04 03:22:31
【问题描述】:

不知何故,我在编译时总是越界。我知道我的数组中有 10 个元素。我尝试将确切的数量作为迭代值和数组的长度。但我还是过去了。

package com.hb.examples;

import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.hb.examples.pinnedsection.R;
import com.hb.views.PinnedSectionListView;
import com.hb.views.PinnedSectionListView.PinnedSectionListAdapter;

public class PinnedSectionListActivity extends ListActivity implements OnClickListener {

    static class SimpleAdapter extends ArrayAdapter<Item> implements PinnedSectionListAdapter {

        private static final int[] COLORS = new int[] {
            R.color.green_light, 
            R.color.orange_light,
            R.color.blue_light, 
            R.color.red_light };

        public SimpleAdapter(Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);

            //final int sectionsNumber = 'Z' - 'A' + 1;
            final int sectionsNumber = 'Z' - 'A' + 1;

            prepareSections(sectionsNumber);

            int sectionPosition = 0, listPosition = 0;

            for (int i=0; i< sectionsNumber; i++) {

                String title = null;

                final String []country = {
                        "Korean", "Japanese", "Chinese", "Cambodian", "Loas", "Taiwamese"
                };


                final String [] CATEGORY = {
                    "Language",
                    "sports",
                    "love",
                    "luxury",
                    "vacation",
                    "games",
                    "home",
                    "travel",
                    "electronics",
                    "movies",
                };

                switch (('A' + i)) {
                case ('A' + 0):
                    title = country[0];
                    break;
                case ('A' + 1):
                    title = country[1];
                    break;
                case ('A' + 2):
                    title = country[2];
                    break;
                case ('A' + 3):
                    title = country[3];
                    break;
                case ('A' + 4):
                    title = country[4];
                    break;
                case ('A' + 5):
                    title = country[5];
                    break;
                default:
                    break;
                }

                //Create a new Item class with section header and Name
                Item section = new Item(Item.SECTION, title + i);
                //Item section = new Item(Item.SECTION, String.valueOf((char)('A' + i)));

                section.sectionPosition = sectionPosition;
                section.listPosition = listPosition++;
                onSectionAdded(section, sectionPosition);
                add(section);

                final int itemsNumber = CATEGORY.length; 

                //(int) Math.abs((Math.cos(2f*Math.PI/3f * sectionsNumber / (i+1f)) * 25f));

                // For loop to iterate the exact number of itemNumber
                for (int j = 0;j < CATEGORY.length;j++) {
                    //Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.KOREA) + " - " + j);
                    Item item = new Item(Item.ITEM, CATEGORY[i]);
                    item.sectionPosition = sectionPosition;
                    item.listPosition = listPosition++;
                    add(item);
                }

                sectionPosition++;
            }
        }

这是 logcat 的输出。

03-03 22:15:55.075: E/AndroidRuntime(19731): FATAL EXCEPTION: main
03-03 22:15:55.075: E/AndroidRuntime(19731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hb.examples.pinnedsection/com.hb.examples.PinnedSectionListActivity}: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.os.Looper.loop(Looper.java:213)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread.main(ActivityThread.java:5225)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at java.lang.reflect.Method.invoke(Method.java:525)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at dalvik.system.NativeStart.main(Native Method)
03-03 22:15:55.075: E/AndroidRuntime(19731): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.hb.examples.PinnedSectionListActivity$SimpleAdapter.<init>(PinnedSectionListActivity.java:124)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.hb.examples.PinnedSectionListActivity.initializeAdapter(PinnedSectionListActivity.java:338)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.hb.examples.PinnedSectionListActivity.initializeHeaderAndFooter(PinnedSectionListActivity.java:326)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at com.hb.examples.PinnedSectionListActivity.onCreate(PinnedSectionListActivity.java:242)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.Activity.performCreate(Activity.java:5133)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-03 22:15:55.075: E/AndroidRuntime(19731):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)

【问题讨论】:

    标签: android arrays indexoutofboundsexception


    【解决方案1】:

    您的问题似乎在这里:

     for (int j = 0;j < CATEGORY.length;j++) {
           //Item item = new Item(Item.ITEM, section.text.toUpperCase(Locale.KOREA) + " - " + j);
           Item item = new Item(Item.ITEM, CATEGORY[i]);
    

    您使用变量j 进行迭代,但您使用i 作为索引。

    你巨大的switch代码也可以像这样减少

    if (i<=5)
        title = country[i];
    

    另外,LogCat 说它无法创建 Activity,所以问题可能出在您的 onCreate() 方法上。如果您正在调用您从 onCreate 发布的代码,那么我在这里写的内容可以解决您的问题,但也许您正在执行一些您没有发布的其他数组操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 2016-01-04
      相关资源
      最近更新 更多