gpt4 book ai didi

Android 应用错误 "Unfortunately App has Stopped"

转载 作者:搜寻专家 更新时间:2023-11-01 08:04:36 26 4
gpt4 key购买 nike

我正在学习 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com