2

我在退出我的应用程序时遇到了这个问题:当您单击“返回”按钮时,会出现 LeadBolt ExitAd。然后您单击“否”,应用程序退出并显示“此应用程序已停止”的 toast。

所以,有一些广告或将 MediaPlayer 设置为空。

代码如下所示:

alarm = new MediaPlayer();
...     
@Override
    public void onPause() {
        super.onPause();
        if (alarm != null) {
            alarm.release();
            alarm = null;
        }

    }

    @Override
    public void onStop() {
        super.onStop();
        if (alarm != null) {
            alarm.release();
            alarm = null;
        }

    }

    public void onDestroy() {
        myController.destroyAd();
//      myController2.destroyAd();
        if (adView != null) {
              adView.destroy();
            }
        super.onDestroy();
        if (alarm != null) {
            alarm.release();
            alarm = null;
        }
    }

异常日志:

11-05 11:06:49.130: E/AndroidRuntime(2019): FATAL EXCEPTION: main
11-05 11:06:49.130: E/AndroidRuntime(2019): java.lang.RuntimeException: Unable to destroy activity {com.klangstudios.car_pilot_remote/com.my_company.my_app.MainActivity}: java.lang.NullPointerException
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3451)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread.access$1200(ActivityThread.java:141)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.os.Looper.loop(Looper.java:137)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at java.lang.reflect.Method.invoke(Method.java:511)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at dalvik.system.NativeStart.main(Native Method)
11-05 11:06:49.130: E/AndroidRuntime(2019): Caused by: java.lang.NullPointerException
11-05 11:06:49.130: E/AndroidRuntime(2019):     at com.klangstudios.car_pilot_remote.MainActivity.onDestroy(MainActivity.java:209)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.Activity.performDestroy(Activity.java:5273)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1110)
11-05 11:06:49.130: E/AndroidRuntime(2019):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3438)
11-05 11:06:49.130: E/AndroidRuntime(2019):     ... 11 more

有人遇到过类似的问题吗?

4

1 回答 1

2


在您的代码中,您应该替换以下内容:

    myController.destroyAd();

有了这个:

 if(myController!= null) {
      myController.destroyAd();
}

然后尝试一下。您将避免出现空指针异常,因为如果 myController 为空,则程序不会破坏不存在的东西

于 2013-11-05T11:29:06.390 回答