2012-12-21 62 views
1

我已經創建了新的活動,我的MainActivity應該午餐,爲什麼應用程序崩潰在新活動(稱爲GamePlayActivity)的開始。 這裏是Java代碼:開始活動致命異常Android

Intent startGameDrill = new Intent(MainActivity.this, GamePlayActivity.class); 
    startActivity(startGameDrill); 

這裏是startGameDrill:

package com.simplemathgame; 

import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class GamePlayActivity extends MainActivity { 
    int addDrills; 
    int subDrils; 
    int mulDrills; 
    int divDrills; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game_play); 

     try { 
      numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number); 
      numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number); 
      numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number); 
      numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number); 
      minBoundText = (TextView) findViewById(R.id.min_text); 
      maxBoundText = (TextView) findViewById(R.id.max_text); 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      Log.w("game","error"); 
     } 

     try { 
      addDrills = Integer.parseInt((String) numberOfAddDrills.getText()); 
      subDrils = Integer.parseInt((String) numberOfSubDrills.getText()); 
      mulDrills = Integer.parseInt((String) numberOfMulDrills.getText()); 
      divDrills = Integer.parseInt((String) numberOfDivDrills.getText()); 
     } catch (NumberFormatException e) { 
      Log.w("GameDrills","string to int"); 
     } 
     Log.w("add", "" + addDrills); 
     Log.w("add", "" + subDrils); 
     Log.w("add", "" + mulDrills); 
     Log.w("add", "" + divDrills); 
    } 

} 

這裏是清單:

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.simplemathgame.Splash" 
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.simplemathgame.MainActivity" 
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
      <intent-filter> 
       <action android:name="com.simplemathgame.MainActivity" /> 

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

     <activity 
      android:name="com.simplemathgame.GamePlayActivity" 
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > 
     </activity> 
    </application> 

</manifest> 

這裏是logcat的:

12-21 22:05:53.949: D/dalvikvm(610): GC_EXTERNAL_ALLOC freed 42K, 53% free 2546K/5379K, external 1917K/2137K, paused 41ms 
12-21 22:06:32.809: D/AndroidRuntime(610): Shutting down VM 
12-21 22:06:32.809: W/dalvikvm(610): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-21 22:06:32.818: E/AndroidRuntime(610): FATAL EXCEPTION: main 
12-21 22:06:32.818: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemathgame/com.simplemathgame.GamePlayActivity}: java.lang.NullPointerException 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method) 
12-21 22:06:32.818: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:507) 
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-21 22:06:32.818: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-21 22:06:32.818: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method) 
12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException 
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
12-21 22:06:32.818: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
12-21 22:06:32.818: E/AndroidRuntime(610): ... 11 more 

這是佈局的GamePlayActivity:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:background="#000044"> 
    <TableRow 
     android:layout_marginTop="10dp" 
     android:gravity="center_vertical" > 
     <TextView 
      android:textColor="#FFFFFFFF" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:text="Addition Drills:"/> 
    </TableRow>  
</TableLayout> 

它爲什麼崩潰?

+0

更改日誌貓,抱歉出錯 – vlio20

+0

您的佈局xml是什麼樣的?另外一個提示:不要在你的Int解析代碼中強制轉換爲字符串 - 「getText()。toString()'方法存在。 –

回答

4

看一看這裏:

12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException 
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31) 

GamePlayActivity.java 31是這一行:

addDrills = Integer.parseInt((String) numberOfAddDrills.getText()); 

因爲在這條線,你引用一個成員或方法的是numberOfAddDrills唯一的東西,那麼它必須爲空。

考慮您在使用findViewById時檢查異常;然而,如果沒有找到視圖,它只會返回null,而不是拋出異常。

看看你發佈的activity_game_play.xml;沒有TextViewandroid:id="@+id/add_drills_number"。你需要創建它,併爲其他五個TextView s同樣的交易。

哦,還有一個提示:如果你沒有讀取日誌的意圖,不要僅僅記錄} catch (Exception e1) {,尤其是的通用異常。我無法告訴你有多少人錯過了這樣做的錯誤。

如果你想從另一個佈局傳遞值,你有兩個選擇:保持對View對象的引用(這有點傻,不要這樣做);或者你從他們所需要的數據傳遞到新Activity

Bundle extras = getIntent().getExtras(); 
int myIntExtra = extras.getInt("myIntExtra"); 

然後使用值,而不是:

startGameDrill.putExtra("myIntExtra", 5); // For example 

然後,在GamePlayActivity.onCreate(),您可以使用getIntent()的額外讀取它。依靠先前佈局中的視圖。有一個更完整的例子in this answer

+0

擊敗我3秒... – Sam

+0

你們太快了! –

+1

@Sam補償你擊敗我的次數! :D – Eric

0

按照堆棧跟蹤,直到你找到一些引用您的類:

它顯示:

12-21 22:06:32.818: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException 
12-21 22:06:32.818: E/AndroidRuntime(610): at com.simplemathgame.GamePlayActivity.onCreate(GamePlayActivity.java:31) 

所以,你要創建一個NullPointerException在GamePlayActivity.java

第31行第31行:

addDrills = Integer.parseInt((String) numberOfAddDrills.getText()); 

所以也許numberOfAddDrills爲空?

+0

更改日誌貓,抱歉的錯誤 – vlio20

+0

更新的答案;) – micha

+0

也許鑄造到一個字符串返回一個'null'? –