简体   繁体   中英

Getting ArrayIndexOutOfBoundsException when accessing list

I am trying to call a class that calls a list. From my original list item when selected. So pretty much a list within a list, and I am getting an array out of bounce errors. The error the logcat displays is ArrayIndexOutOfBoundsException.

I originally just want my list values to call more values from another list. I know there is probably an easier way to do this, but I haven't stumbled across it yet. This cerealBarApps class is 1 of 27 classes I am trying to do this for.

package com.cerealBarApps;

import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle cerealBarApps) {
        super.onCreate(cerealBarApps);

        final String[] DepartmentNames = { "a",
                "b", "c",
                "d", "e",
                "f",
                "g", "h",
                "i", "j",
                "k", "l", "m", "n",
                "o",
                "o",
                "q", "r",
                "s" };

        String[] departments = getResources().getStringArray(
                R.array.departments_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                departments));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String openDepartment = DepartmentNames[position];

                try {
                    Class selected = Class.forName("com.cerealBarApps"
                            + openDepartment);
                    Intent selectedIntent = new Intent(/*this, selected*/);
                    startActivity(selectedIntent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    };
};

The class I'm trying to call is AboutCerealBarApps

package com.cerealBarApps;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class AboutCerealBarApps extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);

        String[] cerealBarApps = getResources().getStringArray(
                R.array.cerealBarApps_array);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                cerealBarApps));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);



        lv.setOnItemClickListener(new OnItemClickListener(){

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                Toast.LENGTH_SHORT).show();
    }
    });

};
};

My String.xml is where I stored my array values.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, HelloListViewActivity!</string>
    <string name="app_name">HelloListView</string>

    <string-array name="departments_array">
        <item>B</item>
        <item>A(AFROTC)</item>
        <item>O</item>
        <item>H</item>
        <item>C</item>
        <item>E</item>
        <item>M</item>
        <item>P</item>
        <item>E</item>
        <item>M</item>
        <item>T</item>
        <item>P</item>
        <item>B</item>
        <item>H</item>
        <item>E</item>
        <item>R</item>
        <item>A</item>
        <item>M</item>
        <item>A</item>
        <item>C</item>
        <item>I</item>
        <item>I</item>
        <item>M</item>
        <item>About Cereal Bar Apps</item>
    </string-array>


    <string-array name="cerealBarApps_array">
        <item>Ex</item>
        <item>xi</item>
        <item>An App Within an App</item>
        <item>BADUMM!!!!!</item>
        <item>In Your Soup</item>
        <item>GGWP</item>
         <item>Ebenezer Ackon</item>
        <item>Ernest Grzybowski</item>
        <item>An App Within an App</item>
        <item>BADUMM!!!!!</item>
        <item>In Your Soup</item>
        <item>GGWP</item>
         <item>Ex</item>
        <item>xki</item>
        <item>An App Within an App</item>
        <item>BADUMM!!!!!</item>
        <item>In Your Soup</item>
        <item>GGWP</item>
         <item>Exn</item>
        <item>Exi</item>
        <item>An App Within an App</item>
        <item>BADUMM!!!!!</item>
        <item>In Your Soup</item>
        <item>GGWP</item>
            </string-array>

</resources>

My Logcat

12-17 12:48:26.072: E/AndroidRuntime(229): Uncaught handler: thread main exiting due to uncaught exception
12-17 12:48:26.082: E/AndroidRuntime(229): java.lang.ArrayIndexOutOfBoundsException
12-17 12:48:26.082: E/AndroidRuntime(229):  at com.cerealBarApps.HelloListViewActivity$1.onItemClick(HelloListViewActivity.java:43)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.widget.ListView.performItemClick(ListView.java:3285)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.os.Handler.handleCallback(Handler.java:587)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.os.Looper.loop(Looper.java:123)
12-17 12:48:26.082: E/AndroidRuntime(229):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-17 12:48:26.082: E/AndroidRuntime(229):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 12:48:26.082: E/AndroidRuntime(229):  at java.lang.reflect.Method.invoke(Method.java:521)
12-17 12:48:26.082: E/AndroidRuntime(229):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-17 12:48:26.082: E/AndroidRuntime(229):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-17 12:48:26.082: E/AndroidRuntime(229):  at dalvik.system.NativeStart.main(Native Method)
12-17 12:48:26.094: I/dalvikvm(229): threadid=7: reacting to signal 3
12-17 12:48:26.094: E/dalvikvm(229): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

This is the code on line 43 - 46 lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { String openDepartment = DepartmentNames[position];

ArrayIndexOutOfBounds exception occurs when you pass an index to an array which doesn't exist.

If I have an array { "One", "two", "three" } the valid indexes will be 0, 1 and 2.

If I have a loop or method function which tries to access the array like this:

String string = myArray[-1] //ArrayIndexOutOfBoundsException
String string = myArray[0] //string="One"
String string = myArray[1] //string="two"
String string = myArray[2] //string="three"
String string = myArray[3] //ArrayIndexOutOfBoundsException

Any indexes that are not valid will pass this exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM