2013-11-24 65 views
-1

當我剛接觸android並且最近創建了一些簡單的應用程序時,我決定做一個快照遊戲,但是當我嘗試運行它在AVD上時,我得到這個錯誤Unfortunately ProjectThing has stoppedAndroid eclipse:不幸的是,ProjectThing已經停止,錯誤

主要代碼:

`package com.example.snapgame; 

import java.util.Random; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    public TextView t1, t2, t3; 
    public Button b1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     t1 = (TextView) findViewById(R.id.textView1); 
     t2 = (TextView) findViewById(R.id.textView2); 
     t3 = (TextView) findViewById(R.id.textView3); 
     b1 = (Button) findViewById(R.id.button1); 

     Snap(); 

     b1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int score = 0; 
       if(t1.equals(t2)){ 
        score++; 
        t3.setText(score); 
       } 

      } 
     });{ 

     } 
    } 

     private void Snap() { 

      String aWords1[] = {"Red", "Blue", "Green", "Yellow"}; 
      String aWords2[] = {"Red", "Blue", "Green", "Yellow"}; 
      String randomColor1 = (aWords1[new Random().nextInt()]); 
      String randomColor2 = (aWords2[new Random().nextInt()]); 

      t1.setText(randomColor1); 
      t2.setText(randomColor2); 

     } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

}` 

主要layout.XML:

`<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_marginLeft="18dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="35dp" 
     android:layout_marginTop="64dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="89dp" 
     android:text="Snap!" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="56dp" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout>` 

的Android的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.snapgame" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

而且logcat的:

11-24 10:36:25.967: D/AndroidRuntime(1153): Shutting down VM 
11-24 10:36:25.967: W/dalvikvm(1153): threadid=1: thread exiting with uncaught exception (group=0xb3ad8b90) 
11-24 10:36:25.977: E/AndroidRuntime(1153): FATAL EXCEPTION: main 
11-24 10:36:25.977: E/AndroidRuntime(1153): Process: com.example.snapgame, PID: 1153 
11-24 10:36:25.977: E/AndroidRuntime(1153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.snapgame/com.example.snapgame.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=-689701214 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.os.Looper.loop(Looper.java:137) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at dalvik.system.NativeStart.main(Native Method) 
11-24 10:36:25.977: E/AndroidRuntime(1153): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=-689701214 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at com.example.snapgame.MainActivity.Snap(MainActivity.java:47) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at com.example.snapgame.MainActivity.onCreate(MainActivity.java:25) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.Activity.performCreate(Activity.java:5243) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
11-24 10:36:25.977: E/AndroidRuntime(1153):  ... 11 more 
11-24 10:36:28.937: I/Process(1153): Sending signal. PID: 1153 SIG: 9 
11-24 10:39:08.297: D/AndroidRuntime(1201): Shutting down VM 
11-24 10:39:08.297: W/dalvikvm(1201): threadid=1: thread exiting with uncaught exception (group=0xb3ad8b90) 
11-24 10:39:08.317: E/AndroidRuntime(1201): FATAL EXCEPTION: main 
11-24 10:39:08.317: E/AndroidRuntime(1201): Process: com.example.snapgame, PID: 1201 
11-24 10:39:08.317: E/AndroidRuntime(1201): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.snapgame/com.example.snapgame.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=-1345881455 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.os.Looper.loop(Looper.java:137) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at dalvik.system.NativeStart.main(Native Method) 
11-24 10:39:08.317: E/AndroidRuntime(1201): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=-1345881455 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at com.example.snapgame.MainActivity.Snap(MainActivity.java:47) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at com.example.snapgame.MainActivity.onCreate(MainActivity.java:25) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.Activity.performCreate(Activity.java:5243) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
11-24 10:39:08.317: E/AndroidRuntime(1201):  ... 11 more 
11-24 10:39:10.557: I/Process(1201): Sending signal. PID: 1201 SIG: 9 
11-24 10:39:52.907: D/AndroidRuntime(1248): Shutting down VM 
11-24 10:39:52.907: W/dalvikvm(1248): threadid=1: thread exiting with uncaught exception (group=0xb3ad8b90) 
11-24 10:39:52.927: E/AndroidRuntime(1248): FATAL EXCEPTION: main 
11-24 10:39:52.927: E/AndroidRuntime(1248): Process: com.example.snapgame, PID: 1248 
11-24 10:39:52.927: E/AndroidRuntime(1248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.snapgame/com.example.snapgame.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=4; index=174154180 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.os.Looper.loop(Looper.java:137) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at java.lang.reflect.Method.invoke(Method.java:515) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at dalvik.system.NativeStart.main(Native Method) 
11-24 10:39:52.927: E/AndroidRuntime(1248): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=174154180 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at com.example.snapgame.MainActivity.Snap(MainActivity.java:47) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at com.example.snapgame.MainActivity.onCreate(MainActivity.java:25) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.Activity.performCreate(Activity.java:5243) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
11-24 10:39:52.927: E/AndroidRuntime(1248):  ... 11 more 
+0

不幸的是,StackOverflow並沒有被設計成學習人們閱讀StackTrace的地方。 –

回答

3

首先什麼是ArrayIndexOutOfBoundsException

拋出以指示已使用非法 索引訪問數組。該索引或者是負值,或者大於或等於該陣列的大小 。

如果你檢查你的logcat你會看到它拋出這個異常的行:

at com.example.snapgame.MainActivity.Snap(MainActivity.java:47) 

所以你要看看你的MainActivity的java文件的第47行。


如何解決這個問題?

它在這裏:

String randomColor1 = (aWords1[new Random().nextInt()]); 
String randomColor2 = (aWords2[new Random().nextInt()]); 

應該

String randomColor1 = (aWords1[new Random().nextInt(aWords1.length)]); 
String randomColor2 = (aWords2[new Random().nextInt(aWords2.length)]); 

爲什麼?

因爲nextInt()返回僞隨機均勻分佈int。或者int的可能值在-2^312^31-1之間。

或者您的陣列可能的索引從0yourArray.length - 1

由於兩個數組的長度是從03,你的情況,你會拋出此異常時nextInt()返回比0低於或高於3優越的整數。


幸運的是,API爲您提供了一種方法!

你必須使用重載方法nextInt(int n)它返回一個僞隨機均勻地分佈在半開區間[0, n)int(裝置0包括和n 包括在內)。

相關問題