Java 为什么我的应用程序会在我单击按钮后关闭?

Java 为什么我的应用程序会在我单击按钮后关闭?,java,android,Java,Android,我正在为android开发一个应用程序,这是一个测验。在第页上有两个按钮。一个工作,一个不工作。当我打开emulator并按下按钮时,应用程序关闭,出现以下消息:“应用程序意外关闭”,我不知道我缺少了什么。以下是清单和三项活动: 以下是带有两个按钮的Java文件: package com.saifandvarun.quizofidiocy; import android.os.Bundle; import android.view.View; import android.widget.Butt

我正在为android开发一个应用程序,这是一个测验。在第页上有两个按钮。一个工作,一个不工作。当我打开emulator并按下按钮时,应用程序关闭,出现以下消息:“应用程序意外关闭”,我不知道我缺少了什么。以下是清单和三项活动: 以下是带有两个按钮的Java文件:

package com.saifandvarun.quizofidiocy;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle iLikeTennis) {
        super.onCreate(iLikeTennis);
        setContentView(R.layout.activity_main);

        Button failstart = (Button) findViewById(R.id.loser);
        failstart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.saifandvarun.quizofidiocy.FAILSCREEN"));
            }
        });

        Button truestart = (Button) findViewById(R.id.the_quiz);
        truestart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("com.saifandvarun.quizofidiocy.FIRSTQUESTION"));
            }
        });

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
}
以下是匹配的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:background="@drawable/home_back" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="Welcome To The Quiz Of Idiocy."
        android:textColor="#DF013A"
        android:textSize="21dp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="34dp"
        android:text="The rules are simple, get the question right, you move on, get it wrong, you&apos;re an idiot. (Oh yeah, and you lose)"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/the_quiz"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="102dp"
        android:text="Just A Pointless Button..."
        android:textSize="25dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/loser"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="START!!"
        android:textColor="#00FF00"
        android:textSize="34dp"
        android:textStyle="bold|italic" /> 

</RelativeLayout>
以下是匹配的xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>
以及相应的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="LOSER!!! You lost, Idiot!"
        android:textColor="#ff0000"
        android:textSize="70dp"
        android:textStyle="bold" >
    </TextView>

    <Button
        android:id="@+id/tryagain"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="22dp"
        android:text="Try Again???"
        android:textColor="#ffffff"
        android:textStyle="bold|italic" >
    </Button>

</RelativeLayout>        

最后,这里是最主要的:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.saifandvarun.quizofidiocy"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

        <activity
            android:name=".Failscreen"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.saifandvarun.quizofidiocy.FAILSCREEN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Firstquestion"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.saifandvarun.quizofidiocy.FIRSTQUESTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>


我不知道我错过了什么。请帮帮我。

您的问题是您从未在
Failscreen
中初始化
MediaPlayer
。它正在
onCreate()
中抛出
NullPointerException
,这就是它说无法创建
活动的原因

public class Failscreen extends Activity {
    MediaPlayer whawHawha;//please initialize me before you attempt to call me!

    @Override
    protected void onCreate(Bundle iLikeTabletennis) {
        // TODO Auto-generated method stub
        super.onCreate(iLikeTabletennis);
        setContentView(R.layout.failscreen);
        MediaPlayer.create(Failscreen.this, R.raw.lose_sound);


        whawHawha.start(); //I am causing a NullPointerException
        //because I am never initialized!


        Button retry = (Button) findViewById(R.id.tryagain);
        retry.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.MAIN"));
            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        whawHawha.release();
    }
}

您的问题是您从未在
Failscreen
中初始化
MediaPlayer
。它正在
onCreate()
中抛出
NullPointerException
,这就是它说无法创建
活动的原因

public class Failscreen extends Activity {
    MediaPlayer whawHawha;//please initialize me before you attempt to call me!

    @Override
    protected void onCreate(Bundle iLikeTabletennis) {
        // TODO Auto-generated method stub
        super.onCreate(iLikeTabletennis);
        setContentView(R.layout.failscreen);
        MediaPlayer.create(Failscreen.this, R.raw.lose_sound);


        whawHawha.start(); //I am causing a NullPointerException
        //because I am never initialized!


        Button retry = (Button) findViewById(R.id.tryagain);
        retry.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.MAIN"));
            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        whawHawha.release();
    }
}

发布logcat堆栈跟踪。可能是因为它不太礼貌
android:text=“LOSER!!!你输了,白痴!”
hehehe:)很多代码,但是重要的logcat丢失了。叹气。发布logcat堆栈跟踪。可能是因为这不太礼貌
android:text=“LOSER!!!你输了,白痴!”
hehehe:)很多代码,但重要的logcat丢失了。叹气。你一定是某种形式的黑魔法艺术家,你看到他的logcat痕迹的方式甚至没有他张贴。你一定是某种形式的黑魔法艺术家,你看到他的logcat痕迹的方式甚至没有他张贴。
public class Failscreen extends Activity {
    MediaPlayer whawHawha;//please initialize me before you attempt to call me!

    @Override
    protected void onCreate(Bundle iLikeTabletennis) {
        // TODO Auto-generated method stub
        super.onCreate(iLikeTabletennis);
        setContentView(R.layout.failscreen);
        MediaPlayer.create(Failscreen.this, R.raw.lose_sound);


        whawHawha.start(); //I am causing a NullPointerException
        //because I am never initialized!


        Button retry = (Button) findViewById(R.id.tryagain);
        retry.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent("android.intent.action.MAIN"));
            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        whawHawha.release();
    }
}