Reputation: 38
I'm getting the following logcat output:
11-22 20:57:01.394: E/MediaPlayer(1647): error (1, 0)
11-22 20:57:01.404: D/MediaPlayer(1647): create failed:
11-22 20:57:01.404: D/MediaPlayer(1647): java.io.IOException: Prepare failed.: status=0x1
11-22 20:57:01.404: D/MediaPlayer(1647): at android.media.MediaPlayer.prepare(Native Method)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.media.MediaPlayer.create(MediaPlayer.java:668)
11-22 20:57:01.404: D/MediaPlayer(1647): at com.jesse.abc.Apple.onCreate(Apple.java:25)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.os.Looper.loop(Looper.java:123)
11-22 20:57:01.404: D/MediaPlayer(1647): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 20:57:01.404: D/MediaPlayer(1647): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 20:57:01.404: D/MediaPlayer(1647): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 20:57:01.404: D/MediaPlayer(1647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-22 20:57:01.404: D/MediaPlayer(1647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-22 20:57:01.404: D/MediaPlayer(1647): at dalvik.system.NativeStart.main(Native Method)
11-22 20:57:01.414: D/AndroidRuntime(1647): Shutting down VM
11-22 20:57:01.414: W/dalvikvm(1647): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 20:57:01.414: E/AndroidRuntime(1647): FATAL EXCEPTION: main
11-22 20:57:01.414: E/AndroidRuntime(1647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jesse.abc/com.jesse.abc.Apple}: java.lang.NullPointerException
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.os.Looper.loop(Looper.java:123)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 20:57:01.414: E/AndroidRuntime(1647): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 20:57:01.414: E/AndroidRuntime(1647): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 20:57:01.414: E/AndroidRuntime(1647): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-22 20:57:01.414: E/AndroidRuntime(1647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-22 20:57:01.414: E/AndroidRuntime(1647): at dalvik.system.NativeStart.main(Native Method)
11-22 20:57:01.414: E/AndroidRuntime(1647): Caused by: java.lang.NullPointerException
11-22 20:57:01.414: E/AndroidRuntime(1647): at com.jesse.abc.Apple.onCreate(Apple.java:32)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 20:57:01.414: E/AndroidRuntime(1647): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-22 20:57:01.414: E/AndroidRuntime(1647): ... 11 more
11-22 20:57:02.934: I/Process(1647): Sending signal. PID: 1647 SIG: 9
This is my code:
I'm pretty sure that my Manifest and layout files are ok. The error points to line 32, which is myPlayer.start();
. The crash only happens when I switch back and forth between activities repeatedly during testing. Any help will be appreciated.
package com.jesse.abc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class Car extends Activity{
MediaPlayer mPlayer;
MediaPlayer myPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.cletter);
mPlayer = MediaPlayer.create(this, R.raw.button_sound1);
myPlayer = MediaPlayer.create(this, R.raw.carsound);
final Button bButton = (Button)findViewById(R.id.cbackbutton);
final Button nButton = (Button)findViewById(R.id.cnextbutton);
final ImageButton cButton = (ImageButton)findViewById(R.id.cButton);
final Context ctx = this;
myPlayer.start();
bButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.start();
Intent myIntent = new Intent(v.getContext(), Ball.class);
ctx.startActivity(myIntent);
myPlayer.release();
}
});
nButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer.start();
Intent myIntent2 = new Intent(v.getContext(), Duck.class);
ctx.startActivity(myIntent2);
myPlayer.release();
}
});
cButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
myPlayer.start();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
this.startActivity(new Intent(Car.this, StartingPoint.class));
myPlayer.release();
finish();
}
return true;
}
}
Upvotes: 0
Views: 1035
Reputation: 9252
You just answered your own question. myPlayer is null. Debug and step all the way up to that line, hover over myPlayer, is it null? You say it happens when switching in and out of your app, you may have to switch out, then back in, and then debug up to that line.
Upvotes: 0
Reputation: 15089
Yes, it certainly crashes.
myPlayer.release();
myPlayer=null;
myPlayer.start();
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myPlayer.release();
myPlayer=null;
finish();
}
Your object myPlayer
is null
, so it crashes:)
Upvotes: 3