0

我正在为 android 开发一个应用程序,这是一个测验。在页面上有两个按钮。一种有效,一种无效。当我打开模拟器并按下按钮时,应用程序关闭并出现此消息:“应用程序已意外关闭”我不知道我错过了什么。这是清单和三个活动: 这是带有两个按钮的 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>

这是工作按钮导航到的 java 文件:

package com.saifandvarun.quizofidiocy;
import android.app.Activity;
import android.os.Bundle;

public class Firstquestion extends Activity {

    @Override
    protected void onCreate(Bundle iLikeCricket) {
        // TODO Auto-generated method stub
        super.onCreate(iLikeCricket);
        setContentView(R.layout.firstquestion);
    }
}

这是匹配的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>

这是java非工作按钮应该导航到的文件:

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

public class Failscreen extends Activity {

    MediaPlayer whawHawha;

    @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();

        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();
    }
}

以及相应的 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>

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

4

1 回答 1

3

你的问题是你从来没有初始化你的MediaPlayerin Failscreen。它正在抛出一个NullPointerExceptiononCreate()这就是为什么它说它无法创建Activity.

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();
    }
}
于 2012-08-21T15:55:11.023 回答