-2

我今天上传了一个 Android 应用程序,有人报告了带有 RunTimeException 和 NullPointerException 的崩溃报告。当用户尝试通过使用AlertDialog. 报告如下。

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

这是来自 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);}

在游戏结束时,在一个AlertDialog,当使用正面按钮时,

dialog.dismiss();
Intent i = new Intent(PlayScreen.this,PlayScreen.class);
startActivity(i);
finish();
4

1 回答 1

3

在线 104

big_loop.start()NPE(空指针异常)即将到来,因为它发现big_loopnull. 因此,一种方法是在那里放置一个空选项检查

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

NPE 即将到来,因为big_loop在某些设备上从原始声音文件创建失败。

此外,您需要 stop() 和 release() 媒体播放器,这样它就不会因为此处提到的清理资源失败而失败 - http://developer.android.com/reference/android/media/MediaPlayer.html

所以在你的活动中onStop()

@Override
public void onStop() {
 super.onStop();
 big_loop.stop();
 big_loop.release();
}
于 2013-09-09T20:02:09.320 回答