Frage

I uploaded an android app today & someone reported the crash report with RunTimeException & NullPointerException. It gives a force close when user tries to call an activity from same activity by using a button in AlertDialog. Report is given below.

java.lang.RuntimeException: Unable to start activity
ComponentInfo{pacakageName/packageName.PlayScreen}:     
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2001)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2028)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4508)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at pacakageName.PlayScreen.onCreate(PlayScreen.java:104)
at android.app.Activity.performCreate(Activity.java:4479)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1965)

... 11 more

here is some code from PlayScreen.java,

MediaPlayer big_loop
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.play_screen);
    big_loop = MediaPlayer.create(PlayScreen.this, R.raw.big_loop);

    //Line 104
    big_loop.start();
    big_loop.setLooping(true);}

at the end of game,in an AlertDialog, when using positive button,

dialog.dismiss();
Intent i = new Intent(PlayScreen.this,PlayScreen.class);
startActivity(i);
finish();
War es hilfreich?

Lösung

On line 104

big_loop.start() the NPE (Null Pointer Exception) is coming because it is finding big_loop as null. Hence one way is to put a null option check there

if(big_loop != null){
big_loop.start();
}

The NPE is coming because creation of big_loop from raw sound files is failing on some devices.

Also, you need to stop() and release() mediaplayer, so that it does not fail because of failure of cleaning up resources as mentioned here - http://developer.android.com/reference/android/media/MediaPlayer.html

So in your activity in onStop()

@Override
public void onStop() {
 super.onStop();
 big_loop.stop();
 big_loop.release();
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top