Android 数组边界错误

Android 数组边界错误,android,io,android-intent,Android,Io,Android Intent,我一直在谷歌上搜索和玩弄不同的方法,但仍然没有成功 本质上,我所做的是将意图中的资源id传递给新活动。我从意图中提取int,并尝试使用从意图中解包的上下文和资源id实例化一个对象 打包意图并开始下一个活动的第一个活动: case R.id.KoreanVocabularymenuBButton: Intent openBeginnerVocabularyActivity = new Intent(v.getContext(), KoreanVocabularyActivity

我一直在谷歌上搜索和玩弄不同的方法,但仍然没有成功

本质上,我所做的是将意图中的资源id传递给新活动。我从意图中提取int,并尝试使用从意图中解包的上下文和资源id实例化一个对象

打包意图并开始下一个活动的第一个活动:

case R.id.KoreanVocabularymenuBButton:          
Intent openBeginnerVocabularyActivity = new Intent(v.getContext(), KoreanVocabularyActivity.class); 
openBeginnerVocabularyActivity.putExtra("difficulty", R.raw.beginner_numbers);
startActivity(openBeginnerVocabularyActivity);
break;
第二个活动解压意图并尝试用数据实例化对象:

int resource = getIntent().getExtras().getInt("difficulty");
korean = new KoreanVocabulary(this, 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();
    }
编辑:

索引是一个整数。词汇表[]数组是函数的私有成员。构造函数正在尝试初始化数组

我试图使用logcat,结果弄得有点乱:

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 down 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
我需要资源ID的原因是,我想用特定的文件资源初始化对象。我需要一种识别特定文件的方法。我一直在尝试的方法是在菜单上的开关盒内。用户单击难度级别,将在下一个活动中加载该难度的特定列表。但是,我需要知道用户选择了哪种难度。这就是为什么我要追踪身份证的原因

编辑2:

这是数组的类和构造函数信息

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;
    } 
编辑3:

对不起,各位,我发现了问题。在读取数组时,我不知何故有一个off-by-one引用。我原以为问题出在文件读取上,但正如你们都指出的那样,这很好——你们是对的!对不起

BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
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
}
字符串字=null; 试一试{ while((word=br.readLine())!=null){
词汇表[index++]=word;//如果你只需要资源,你只需要获取上下文(你的活动已经有了上下文)并正常调用它。你不应该传递资源


另外,您并没有完全按照假设传递资源……您传递的是该资源的值,因此最好从您的下一个活动访问该资源

我正在尝试查找构造函数之间的连接

korean = new KoreanVocabulary(this, resource);
还有你的其他代码,但找不到。我猜你得到的错误是IOException…在哪里

BufferedReader br = new BufferedReader(new InputStreamReader(is));
从何处获取其输入?
我确实认为问题的标题有点错误-您确实在两个活动之间传递了资源。这不是代码中的问题(当然,除非logcat将显示不同的内容).*编辑-更不用说,正如其他人已经说过的,R.资源到处都是可用的,不需要在活动之间传递它们…

发生了什么错误?将LogCat输出添加到您的帖子中。另外,您为什么要从意图传递资源ID?您可以在有
上下文的任何地方访问资源。您应该删除从问题中找出解决方案,并在答案中进行描述。您能说明如何初始化数组吗?正如我所说的,这与传递资源无关,您做得很好。在构造函数中的某个地方,您使用了未初始化的数组,或者使用了比您请求的数组小的数组。错误可能与词汇表[]有关数组。我更新了信息——我已经显示了它使用的整个构造函数和数据。原始资源是一个字符串数组?这个数组有多大?我计算出来了——它加载了太多的字符串。一个愚蠢的新手错误。抱歉!我更新了最初的帖子并试图解决这些问题。