[英]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.