Android app强制关闭

时间:2012-08-26 19:34:28

标签: android forceclose

我正在开发一个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];


}

1 个答案:

答案 0 :(得分:1)

您正在 8-String数组中添加九个字符串!删除这一行:

wordList[8] = "Middle Eastern";

或者让你的数组保存 9 字符串:

String wordList[] = new String[9];