Android阵列超出界限异常

时间:2014-03-04 03:22:32

标签: android arrays indexoutofboundsexception

不知何故,当我编译时,我一直走出界限。我知道我的数组中有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)

1 个答案:

答案 0 :(得分:3)

您的问题似乎就在这里:

 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表示它无法创建活动,因此问题可能在您的onCreate()方法上。如果你正在调用你在onCreate上发布的代码,那么我在这里写的内容可以解决你的问题,但也许你正在做一些你没有发布的其他数组操作。