问问题
19892 次
2

我正在学习 android 开发,我正在关注 youtube 上的教程,我正在尝试运行此代码,但我不断收到“不幸的是应用程序已停止”,看起来问题出在我启动 Activity 时“startActivity(menuIntent); " 这是 MainActivity.java:

package com.example.thebasicseries;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;


public class MainActivity extends Activity {

MediaPlayer logoMusic;

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

    logoMusic = MediaPlayer.create(MainActivity.this, R.raw.sound);
    logoMusic.start();

    Thread logoTimer = new Thread(){
        public void run(){
            try {
                sleep(5000);
                Intent menuIntent = new Intent("com.example.thebasicseries.menu");
                startActivity(menuIntent);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally{
                finish();
            }
        }
    };
    logoTimer.start();
}

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


 }

这是 menu.java

 package com.example.thebasicseries;
 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 menu extends Activity {

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

    //Button sound
    final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.button);

    //Setting up the button references
    Button tut1 = (Button) findViewById(R.id.tutorial1);
    Button tut2 = (Button) findViewById(R.id.tutorial2);

    tut1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonSound.start();
            startActivity(new Intent("com.example.thebasicseries.TutorialOne"));
        }
    });     


    tut2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            buttonSound.start();
            startActivity(new Intent("com.example.thebasicseries.TutorialOne"));
        }
    });     




}

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

  }

这是清单:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.thebasicseries.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="com.example.thebasicseries.menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.thebasicseries.menu" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.thebasicseries.TutorialOne"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.thebasicseries.TutorialOne" />

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

</manifest>

错误日志如下:

     04-20 11:13:29.780: E/AndroidRuntime(820): FATAL EXCEPTION: main
     04-20 11:13:29.780: E/AndroidRuntime(820): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.thebasicseries/com.example.thebasicseries.menu}: java.lang.NullPointerException
     04-20 11:13:29.780: E/AndroidRuntime(820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
     04-20 11:13:29.780: E/AndroidRuntime(820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at android.os.Handler.dispatchMessage(Handler.java:99)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at android.os.Looper.loop(Looper.java:137)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at android.app.ActivityThread.main(ActivityThread.java:5039)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at java.lang.reflect.Method.invokeNative(Native Method)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at java.lang.reflect.Method.invoke(Method.java:511)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
   04-20 11:13:29.780: E/AndroidRuntime(820):   at dalvik.system.NativeStart.main(Native Method)
   04-20 11:13:29.780: E/AndroidRuntime(820): Caused by: java.lang.NullPointerException
    04-20 11:13:29.780: E/AndroidRuntime(820):  at com.example.thebasicseries.menu.onCreate(menu.java:35)
    04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.Activity.performCreate(Activity.java:5104)
    04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    04-20 11:13:29.780: E/AndroidRuntime(820):  ... 11 more
    04-20 11:13:33.220: I/Process(820): Sending signal. PID: 820 SIG: 9

谢谢!!`

4

1 回答 1

4

问题在这里:

com.example.thebasicseries.menu.onCreate(menu.java:35)

你得到一个 NullPointerException。

似乎您的布局 XML 中没有 Tutorial2 按钮。

于 2013-04-20T11:35:38.307 回答