[英]Global MediaPlayer declaration not working
每当我想声明一个本地媒体播放器时,它就会崩溃。 我已经用本地方法声明了,这首歌播放得很好。 但是当我想在全球范围内声明它时它不起作用。
package com.example.simplerecipes;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class About extends Activity {
MediaPlayer ColdplaySong;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
MediaPlayer.create(About.this, R.raw.coldplay );
ColdplaySong.start();
Thread Timer = new Thread (){
public void run(){
try{
sleep(5000);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainMenu = new Intent("com.example.simplerecipes.MainActivty");
startActivity(openMainMenu);
}
}
};
Timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ColdplaySong.release();
finish();
}
}
这是 LogCat 中的错误
09-13 08:12:30.004: D/AndroidRuntime(825): Shutting down VM
09-13 08:12:30.004: W/dalvikvm(825): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
09-13 08:12:30.014: E/AndroidRuntime(825): FATAL EXCEPTION: main
09-13 08:12:30.014: E/AndroidRuntime(825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplerecipes/com.example.simplerecipes.About}: java.lang.NullPointerException
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.os.Looper.loop(Looper.java:137)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-13 08:12:30.014: E/AndroidRuntime(825): at java.lang.reflect.Method.invokeNative(Native Method)
09-13 08:12:30.014: E/AndroidRuntime(825): at java.lang.reflect.Method.invoke(Method.java:525)
09-13 08:12:30.014: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-13 08:12:30.014: E/AndroidRuntime(825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-13 08:12:30.014: E/AndroidRuntime(825): at dalvik.system.NativeStart.main(Native Method)
09-13 08:12:30.014: E/AndroidRuntime(825): Caused by: java.lang.NullPointerException
09-13 08:12:30.014: E/AndroidRuntime(825): at com.example.simplerecipes.About.onCreate(About.java:16)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.Activity.performCreate(Activity.java:5133)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-13 08:12:30.014: E/AndroidRuntime(825): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-13 08:12:30.014: E/AndroidRuntime(825): ... 11 more
09-13 08:12:33.154: I/Process(825): Sending signal. PID: 825 SIG: 9
MediaPlayer ColdplaySong;
这声明了一个变量但不初始化它(默认情况下,对象引用将为空)。
MediaPlayer.create(About.this, R.raw.coldplay );
这会创建一个MediaPlayer
实例,但不会在任何地方存储引用。
ColdplaySong.start();
这会调用一个空对象的方法,导致 NullPointerException。
你可能想要的是:
ColdplaySong = MediaPlayer.create(About.this, R.raw.coldplay );
ColdplaySong.start();
您必须使用 final 关键字声明 MediaPlayer 对象。
package com.example.simplerecipes;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class About extends Activity {
final MediaPlayer ColdplaySong =MediaPlayer.create(About.this, R.raw.coldplay );
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
ColdplaySong.start();
Thread Timer = new Thread (){
public void run(){
try{
sleep(5000);
}catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainMenu = new Intent("com.example.simplerecipes.MainActivty");
startActivity(openMainMenu);
}
}
};
Timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ColdplaySong.release();
finish();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.