package com.jonald.mazeonmath;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MainActivity extends Activity {
MediaPlayer logoMusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
MediaPlayer logoMusic = MediaPlayer.create(MainActivity.this,
R.raw.splash_sound2);
logoMusic.start();
Thread logoTimer = new Thread() {
public void run(){
try{
sleep(5000);
Intent menuIntent = new Intent("com.jonald.mazeonmath.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();
}
}
我想在更改布局后停止背景音乐。但是在5secs之后它似乎强制关闭应用程序。但当我删除logoMusic.release它运行正常但音乐继续播放。有人可以像我一样帮助新手吗?
也是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jonald.mazeonmath"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jonald.mazeonmath.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.jonald.mazeonmath.menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.jonald.mazeonmath.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
04-26 20:57:49.149: I/Process(517): Sending signal. PID: 517 SIG: 9
04-26 20:58:01.818: D/dalvikvm(557): GC_EXTERNAL_ALLOC freed 917 objects / 71816 bytes in 80ms
04-26 20:58:07.078: D/AndroidRuntime(557): Shutting down VM
04-26 20:58:07.078: W/dalvikvm(557): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-26 20:58:07.118: E/AndroidRuntime(557): FATAL EXCEPTION: main
04-26 20:58:07.118: E/AndroidRuntime(557): java.lang.RuntimeException: Unable to pause activity {com.jonald.mazeonmath/com.jonald.mazeonmath.MainActivity}: java.lang.NullPointerException
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.access$2500(ActivityThread.java:125)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.os.Looper.loop(Looper.java:123)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-26 20:58:07.118: E/AndroidRuntime(557): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 20:58:07.118: E/AndroidRuntime(557): at java.lang.reflect.Method.invoke(Method.java:521)
04-26 20:58:07.118: E/AndroidRuntime(557): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-26 20:58:07.118: E/AndroidRuntime(557): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-26 20:58:07.118: E/AndroidRuntime(557): at dalvik.system.NativeStart.main(Native Method)
04-26 20:58:07.118: E/AndroidRuntime(557): Caused by: java.lang.NullPointerException
04-26 20:58:07.118: E/AndroidRuntime(557): at com.jonald.mazeonmath.MainActivity.onPause(MainActivity.java:41)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.Activity.performPause(Activity.java:3842)
04-26 20:58:07.118: E/AndroidRuntime(557): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3335)
04-26 20:58:07.118: E/AndroidRuntime(557): ... 12 more
04-26 20:58:09.619: I/Process(557): Sending signal. PID: 557 SIG: 9
答案 0 :(得分:1)
这是因为onPause中的字段logoMusic
为空。尝试添加volatile
修饰符。代码:
Thread logoTimer = new Thread() {
public void run() {
try{
sleep(5000);
Intent menuIntent = new Intent("com.jonald.mazeonmath.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
似乎不正确,因为我认为你不应该从后台线程开始新的活动。使用AsyncTask
的onPostExecute [1]。那么您不应该使用volatile
关键字。
1 - http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result)
编辑:请尝试使用以下代码替换上面的代码(以Thread logoTimer
开头,以logoTimer.start();
结尾):
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Intent menuIntent = new Intent("com.jonald.mazeonmath.MENU");
startActivity(menuIntent);
finish();
}
}.execute();
然后告诉我这是否有效。
编辑2 :它一直隐藏在普通视线中。在setContentView
的行下,您将声明方法变量,而不是设置字段。移除MediaPlayer
,只留下logoMusic = MediaPlayer.create(....)
,它应该有效。
答案 1 :(得分:0)
尝试发布时,徽标音乐可能为空。尝试在调用发布之前添加检查。像
这样的东西if (logoMusic != null) {
logoMusic.release();
}