私はグーグルでさまざまな方法で周りを回っていますが、私はまだ失敗しています。配列の境界にエラーがあります
私がやっていることは、インテント内のリソース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();
}
編集:
インデックスは整数です。 vocabulary []配列は、関数のプライベートメンバーです。コンストラクタが配列を初期化しようとしています。
私は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が必要な理由を。特定のファイルを特定する方法が必要です。私が試みてきたやり方は、メニューのスイッチケース内です。ユーザーが難易度をクリックすると、次のアクティビティーでその難しさのために特定のリストがロードされます。しかし、ユーザーが選択した難易度を知る必要があります。だからこそ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:
申し訳ありませんが、誰もが、私は問題を発見しました。私はどういうわけか、配列から読み込むときに1つのリファレンスで何とかしていました。問題はファイルの読み込みにあると思っていましたが、あなたが指摘したように、それはうまくいきました - あなたは正しいです!ごめんなさい!金利#1の
何が起こりますか?投稿にLogCatの出力を追加します。また、インテントからRecource-IDを渡すのはなぜですか?あなたは 'Context'オブジェクトを持っているあらゆる場所で償却にアクセスすることができます。 –
質問から解決方法を削除し、答えに記述する必要があります。 – murgatroid99