力量关闭的原因是什么?

时间:2013-09-09 19:09:05

标签: android performance android-intent forceclose

我今天上传了一个Android应用程序&有人用RunTimeException报告了崩溃报告&空指针异常。当用户尝试使用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();

1 个答案:

答案 0 :(得分:3)

第104行

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

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

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

此外,您需要停止()和释放()媒体播放器,以便它不会因为清理资源失败而失败,如此处所述 - http://developer.android.com/reference/android/media/MediaPlayer.html

所以在您onStop()

的活动中
@Override
public void onStop() {
 super.onStop();
 big_loop.stop();
 big_loop.release();
}