0

Somehow I keep going out of bounds when I compile. I know I have 10 elements in my array. I have tried putting the exact amount as the iterating value and the length of the array. But still I go over.

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++;
            }
        }

Here is the logcat output.

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 Answer 1

3

Your problem seems to be here:

 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]);

You're using the variable j for iteration, but you're using i as index.

Also your huge switch code can be reduced like this

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

Also, the LogCat says that it's unable to create the Activity, so the problem is probably on your onCreate() method. If you're calling the code you posted from onCreate then what I wrote here could solve your problem, but maybe you're doing some other array manipulation that you didn't post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.