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";

或者让你的数组容纳九个字符串:

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