0

私は Android アプリケーションを開発しており、携帯電話とエミュレーターで実行しようとしました。ただし、毎回強制的に閉じます。これがログです。ありがとう

08-26 15:30:04.807: D/AndroidRuntime(10580): Shutting down VM
08-26 15:30:04.807: W/dalvikvm(10580): threadid=1: thread exiting with uncaught exception (group=0x4001e568)
08-26 15:30:04.823: E/AndroidRuntime(10580): FATAL EXCEPTION: main
08-26 15:30:04.823: E/AndroidRuntime(10580): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.imhungry/com.example.imhungry.ImHungry}: java.lang.ArrayIndexOutOfBoundsException
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.os.Looper.loop(Looper.java:130)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread.main(ActivityThread.java:3703)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at java.lang.reflect.Method.invoke(Method.java:507)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at dalvik.system.NativeStart.main(Native Method)
08-26 15:30:04.823: E/AndroidRuntime(10580): Caused by: java.lang.ArrayIndexOutOfBoundsException
08-26 15:30:04.823: E/AndroidRuntime(10580):    at com.example.imhungry.ImHungry.<init>(ImHungry.java:36)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at java.lang.Class.newInstanceImpl(Native Method)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at java.lang.Class.newInstance(Class.java:1409)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-26 15:30:04.823: E/AndroidRuntime(10580):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1578)
08-26 15:30:04.823: E/AndroidRuntime(10580):    ... 11 more
08-26 15:30:08.018: I/Process(10580): Sending signal. PID: 10580 SIG: 9

これが私が問題を抱えているコードです

package com.example.imhungry;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class ImHungry extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im_hungry);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_im_hungry, menu);
        return true;
    }

    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(8);
    String wordList[] = new String[8];
    {
        wordList[0] = "Mexican";
        wordList[1] = "American";
        wordList[2] = "Barbeque";
        wordList[3] = "Chinese";
        wordList[4] = "Indian";
        wordList[5] = "Italian";
        wordList[6] = "Thai";
        wordList[7] = "Viatnamese";
        wordList[8] = "Middle Eastern";

    }


 String wordToDisplay = wordList[randomInt];


}
4

1 に答える 1

1

8弦配列9つの文字列を追加しています!次の行を削除します。

wordList[8] = "Middle Eastern";

または、9つの文字列を保持するように配列を作成します。

String wordList[] = new String[9];
于 2012-08-26T19:43:56.557 に答える