开发者

Error with array bounds

I have been Googling and toying around with different methods and I am still unsuccessful.

Essentially what I am doing is passing a resource id within an intent to a new activity. The I extract the int from the intent and I attempt to instantiate an object using the context and resource id which I unpacked from the intent.

First activity which packs the intent and starts the next activity:

case R.id.KoreanVocabularymenuBButton:          
Intent openBeginnerVocabularyActivity = new Intent(v.getContext(), KoreanVocabularyActivity.class); 
openBeginnerVocabularyActivity.putExtra("difficulty", R.raw.beginner_numbers);
startActivity(openBeginnerVocabularyActivity);
break;

The second activity unpacks the intent and attempts to instantiate an object with the data:

int resource = getIntent().getExtras().getInt("difficulty");
korean = new KoreanVocabulary(this, resource);

This is the constructor of the object and where the error appears to occur:

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String word = null;

    try {
        while ((word = br.readLine()) != null){
            vocabulary[index++] = word;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

edit:

index is an integer. The vocabulary[] array is a private member of the function. The constructor is attempting to initialize the array.

I tried to use logcat and got a bit of a mess:

I/ActivityManager(   51): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000     cmp=korean.koreanstudy/.MainMenuActivity bnds=[5,234][115,352] }
W/WindowManager(   51): No window to dispatch pointer action 0
W/WindowManager(   51): No window to dispatch pointer action 1
I/ActivityManager(   51): Displayed activity korean.koreanstudy/.MainMenuActivity: 388 ms (total 388 ms)
I/ActivityManager(   51): Starting activity: Intent {   cmp=korean.koreanstudy/.VocabularyMenuActivity }
I/ActivityManager(   51): Displayed activity korean.koreanstudy/.VocabularyMenuActivity: 322 ms (total 322 ms)
I/ActivityManager(   51): Starting activity: Intent { cmp=korean.koreanstudy/.KoreanVocabularyActivity (has extras) }
I/global  (  250): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
D/AndroidRuntime(  250): Shutting do开发者_StackOverflowwn VM
W/dalvikvm(  250): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
E/AndroidRuntime(  250): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime(  250): java.lang.RuntimeException: Unable to start activity   ComponentInfo{korean.koreanstudy/korean.koreanstudy.KoreanVocabularyActivity}:      java.lang.ArrayIndexOutOfBoundsException

The reason I want the ID of the resource is because I want to initialize the object with a specific file resource. I need a way to identify the specific file. The way I have been trying is within a switch case on a menu. The user clicks the difficulty level and the specific list will be loaded for that difficulty in the next activity. However, I need to know which difficulty the user selected. That is why I am trying to keep track of the ID.

edit 2:

This is the class and constructor info for the array.

private int index = 0; 
private int max = 0;
private String[] vocabulary = new String[255];

public KoreanVocabulary(Context context, int resource){
    InputStream is = context.getResources().openRawResource(resource);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String word = null;

    try {
        while ((word = br.readLine()) != null){
            vocabulary[index++] = word;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    max = index;
    index = 0;
    } 

edit 3:

Sorry everyone, I found the problem. I somehow had an off by one reference when reading from the array. I thought the problem was with the file reading but as you have all pointed out that was fine-- you are right! sorry!


If you only want the resource you just need to get the context (your Activity will already have this) and call it normally. You shouldn't be passing the resource.

Also, you aren't exactly passing the resource per say... you are passing the value of that resource thus it is just better to access that from your next activity


BufferedReader br = new BufferedReader(new InputStreamReader(is));

String word = null;

try {
    while ((word = br.readLine()) != null){
        vocabulary[index++] = word; //<!-- POI#1
    }
} catch (IOException e) {
    e.printStackTrace(); //<!-- POI#2
}

Point of interest #1: Where is vocabulary/index coming from? What datatypes are they? index is probably int, but it doesn't hurt to ask.

Point of interest #2: Are you getting an exception? If so, is it the IOException? It not, what's the actual error you're getting? I think you forgot that part in your original post - logcat should be able to give you the information you need.


I was trying to find a connection between the constructor

korean = new KoreanVocabulary(this, resource);

and the rest of your code, but couldn't find any. The error you are getting, i guess, is an IOException... where is

BufferedReader br = new BufferedReader(new InputStreamReader(is));

getting its input from? I do believe the title of the question is somewhat wrong - you do pass resources between the two activities. This is not the problem in the code (unless the logcat will show something different, of course). *edit - not to mention that, as others stated already, the R. resources are available all over, no need to pass them between activities...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜