vlio20
vlio20

Reputation: 9295

Fatal Exception on starting activity Android

I have created new activity that my MainActivity Should lunch, some why the application is crashing on the start of the new activity (called GamePlayActivity). Here is the java code:

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

Here is the 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);
    }

}

Here is the manifest:

<?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>

here is the 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

Here is the layout of the 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>

Why does it crashes?

Upvotes: 1

Views: 520

Answers (2)

Cat
Cat

Reputation: 67502

Have a look here:

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)

Line 31 of GamePlayActivity.java is this line:

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

Since the only thing on this line that you reference a member or method of is numberOfAddDrills, then it must be null.

Consider that you are checking for exceptions when using findViewById; however, if the view is not found it will just return null, not throw an exception.

Have a look at the activity_game_play.xml you posted; there is no TextView with android:id="@+id/add_drills_number". You need to create it, and the same deal for the other five TextViews.

Oh, and a hint: Don't just catch generic exceptions with } catch (Exception e1) {, especially if you have no intent of reading the logs. I can't tell you how many times people have missed errors from doing this.

If you want to pass values from another layout, you have two options: keep a reference to the View objects (this is a bit silly, don't do this); or pass the data you need from them to your new Activity:

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

Then, in GamePlayActivity.onCreate(), you can fetch it using getIntent()'s extras:

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

Then use that value instead of relying on the view in the previous layout. There is a fuller example in this answer.

Upvotes: 4

micha
micha

Reputation: 49572

Follow the stacktrace until you find some references to your classes:

It shows:

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)

So you are creating a NullPointerException at line 31 in GamePlayActivity.java

Line 31:

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

so maybe numberOfAddDrills is null?

Upvotes: 0

Related Questions