我是Android编程的初学者。 我正在通过新的波士顿视频学习它。 但每次我启动我的应用程序它崩溃“不幸的应用程序停止工作” 但是,当我单击确定时,它会将我带到应用程序中的第二个活动。 我无法弄清楚为什么会这样。
这是我的Android Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
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=".StartingPoint"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.skm.firstapp.STARTINGPOINT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.skm.firstapp.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Prefs"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.skm.firstapp.PREFS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".TextPlay"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Email"
android:label="@string/app_name" >
</activity>
<activity
android:name=".Camera"
android:label="Camera Application"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Data"
android:label="Data App"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".OpenedClass"
android:label="Data App"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".AboutUs"
android:label="@string/app_name"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="com.skm.firstapp.ABOUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的logcat:
09-07 05:27:26.522: W/dalvikvm(1032): threadid=1: thread exiting with uncaught exception (group=0xb2b05ba8)
09-07 05:27:26.532: E/AndroidRuntime(1032): FATAL EXCEPTION: main
09-07 05:27:26.532: E/AndroidRuntime(1032): Process: com.skm.firstapp, PID: 1032
09-07 05:27:26.532: E/AndroidRuntime(1032): java.lang.RuntimeException: Unable to pause activity {com.skm.firstapp/com.skm.firstapp.Splash}: java.lang.NullPointerException
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3062)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3017)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2995)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.access$1000(ActivityThread.java:135)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.os.Handler.dispatchMessage(Handler.java:102)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.os.Looper.loop(Looper.java:136)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-07 05:27:26.532: E/AndroidRuntime(1032): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 05:27:26.532: E/AndroidRuntime(1032): at java.lang.reflect.Method.invoke(Method.java:515)
09-07 05:27:26.532: E/AndroidRuntime(1032): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-07 05:27:26.532: E/AndroidRuntime(1032): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-07 05:27:26.532: E/AndroidRuntime(1032): at dalvik.system.NativeStart.main(Native Method)
09-07 05:27:26.532: E/AndroidRuntime(1032): Caused by: java.lang.NullPointerException
09-07 05:27:26.532: E/AndroidRuntime(1032): at com.skm.firstapp.Splash.onPause(Splash.java:38)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.Activity.performPause(Activity.java:5335)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
09-07 05:27:26.532: E/AndroidRuntime(1032): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3048)
09-07 05:27:26.532: E/AndroidRuntime(1032): ... 12 more
这是我的Splash.java文件:
package com.skm.firstapp;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle LW) {
// TODO Auto-generated method stub
super.onCreate(LW);
setContentView(R.layout.splash);
MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent ourIntent = new Intent("com.skm.firstapp.MENU");
startActivity(ourIntent);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
答案 0 :(得分:3)
根据您提供的日志cat:您将收到“NullPointerException”:检查此行:
at com.skm.firstapp.Splash.onPause(Splash.java:38)
所以我认为它是你想要释放MediaPlayer资源的那一行。 但如果它尚未初始化,那么你将得到NullPointerException。
所以在onCreate()方法初始化资源。
PS: 分享完整的logcat文件和 也分享Splash.java
:在添加了splash.java之后:
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle LW) {
// TODO Auto-generated method stub
super.onCreate(LW);
setContentView(R.layout.splash);
MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);
ourSong.start();
你需要将mySong声明为你已经完成的实例变量。
但是你也在onCreate()方法中声明为局部变量,如下一行所示:
MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);
所以改成它:
ourSong = MediaPlayer.create(Splash.this, R.raw.drum);