Whenever i want to declare a local Mediaplayer it just crashes. I have declared in a local method and the song plays perfectly fine. But when i want to declare it globally it does not work.
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();
}
}
here are the errors in the 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;
This declares a variable but does not initialize it (by default, the object reference will be null).
MediaPlayer.create(About.this, R.raw.coldplay );
This creates a MediaPlayer
instance but does not store the reference anywhere.
ColdplaySong.start();
This calls a method on a null object, causing NullPointerException.
What you probably want is:
ColdplaySong = MediaPlayer.create(About.this, R.raw.coldplay );
ColdplaySong.start();
You have to declare the MediaPlayer object using final keyword .
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.