Android应用程序错误 "Unfortunately App has Stopped"

标签 android android-intent

我正在学习android开发,我正在遵循youtube上的教程,我正在尝试运行此代码,但我不断收到“不幸的是应用程序已停止”,看起来问题出在我启动 Activity “startActivity(菜单 Intent );”这是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();
}

  }

这是 list :

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

谢谢!! `

最佳答案

问题出在这里:

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

您收到 NullPointerException。

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

关于Android应用程序错误 "Unfortunately App has Stopped",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16119563/

相关文章:

java - 在 Android 中使用 Tabhost 设置选项卡时遇到问题?

android - 如何在android中将图像复制到SD卡

java - Intent.putExtras 大小限制?

android - 从 RecyclerView Adapter 开始新的 Intent

java - 服务背景

android - Andengine 为 sprite 中的每个像素设置 alpha

android - 如何正确触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent ?

android - 单击通知时获取 PendingIntent 事件

java - 如何按升序排序 ArrayList<Object> android

Android:停止 Intent 并返回到主要 Activity