简体   繁体   中英

Android Application Force Crash at main Activity Launch

I just started working on this application I feel like I've done everything right (This isn't my first application I've been working on) but when I try to run it It crashes?? It gives me no errors.

Here's the code from the main activity, then from manufest and then from the xml layout. `

     package com.theory.game;
     import java.util.Random;
     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.View;
     import android.widget.Button;

    public class GameTheoryActivity extends Activity implements View.OnClickListener{

    Button play, settings;
    int i;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        play.setOnClickListener(this);
        settings.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bPlay:

            Random irand = new Random();
            i = irand.nextInt(4);


            switch(i){
            case 0:

                Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class);
                startActivity(GoMillionair);
                return;
            case 1:
                return;
            case 2:

                return;
            case 3:

                return; 
            }

            return;
        case R.id.bSettings:
            return; 
        }
    }
}

Manufest:

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

    <uses-sdk android:minSdkVersion="15" />

    <application >
        <activity
            android:name=".GameTheoryActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".millionair" >
            <intent-filter>
                <action android:name="com.theory.game.MILLIONAIR" />

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

</manifest>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundimage"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="390dp"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <Button
            android:id="@+id/bPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.38"
            android:background="@drawable/play" />

        <Button
            android:id="@+id/bSettings"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:layout_weight="0.36"
            android:background="@drawable/settings" />
    </LinearLayout>

</LinearLayout> 

`

And I have another class called millionair which is just a normal class nothing much, nothing wrong there I guess. Please check whats up with this and let me know why my application wont start saying it "has stopped working".. Thanks

My guess is that you are getting a NullPointerException because both play and settings objects are null.

change your onCreate() to look like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    play = (Button)findViewById(R.id.bPlay);
    settings = (Button)findViewById(R.id.bSettings);

    play.setOnClickListener(this);
    settings.setOnClickListener(this);

}

You'll probably get a NullPointerException because you forgot to assign the buttons to your fields ( Button play, settings; ).

Here's an example on how to assign them:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    play = (Button) findViewById(R.id.bPlay);
    settings = (Button) findViewById(R.id.bSettings);
}

Looks like play and settings will both be null when you try to use then during the onCreate() method:

play.setOnClickListener(this);
settings.setOnClickListener(this);

make sure to initiate them like so:

play = (Button)findViewById(R.id.bPlay);
settings = (Button)findViewById(R.id.bSettings)
play.setOnClickListener(this);
settings.setOnClickListener(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM