使用throws

时间:2017-03-09 12:01:13

标签: android exception-handling

这个方法在某些情况下有Nullpointer Exception,我使用throws来捕获这个异常,但它没有捕获,我在做错了什么???

方法: -

void soundAtPlayOneForssahAnimation() throws NullPointerException{
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                releaseResource();
                mp_button_Click = (MediaPlayer.create(getActivity(), R.raw.forssah_button_clicked_sound_points));
                mp_button_Click.start();
                mp_button_Click.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mp.stop();
                        mp.reset();
                        mp.release();
                        mp_button_Click = null;
                    }
                });
            }
        }, 100);

        //wait for .7s to match the duration
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                releaseResource();
                mp_button_Click = MediaPlayer.create(getActivity(), R.raw.coupon_rotation_sound_thick_tope_five_seconds);
                mp_button_Click.start();
            }
        }, 700);

        //wait for the coupon rotation finished
        couponRotationFinishedMusicHandler = new Handler();
        couponRotationFinishedMusicHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                releaseResource();
                mp_button_Click = MediaPlayer.create(getActivity(), R.raw.coupon_rotation_finished_sound_arcade_reveal);
                mp_button_Click.start();
                couponRotationFinishedMusicHandler = null;

            }
        }, 6500);

        //wait for the timer start

        timerCountMusicHandler = new Handler();
        timerCountMusicHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                releaseResource();
                mp_button_Click = MediaPlayer.create(getActivity(), R.raw.timer_count_sound_quiz_thinking);
                mp_button_Click.start();
                timerCountMusicHandler = null;
            }
        }, 7300);


        //wait for the timer till 5sec remaining
        warningTimerCountMusicHandler = new Handler();
        warningTimerCountMusicHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                warningTimerCountMusic = MediaPlayer.create(getActivity(), R.raw.warning_timer_sound_second_countdown);
                warningTimerCountMusic.start();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //releaseResource();
                        if (warningTimerCountMusic != null) {
                            warningTimerCountMusic.release();
                            warningTimerCountMusic = null;
                            if (mp_button_Click != null) {
                                mp_button_Click.release();
                                mp_button_Click = null;
                                warningTimerCountMusicHandler = null;
                            }
                        }
                    }
                }, 5900);
            }
        }, (sessionTimeOut + 2) * 1000);
    }

这就是我所说的: -

try {
        soundAtPlayOneForssahAnimation();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

我的问题是它没有陷入阻挡

日志: -

  

03-09 15:18:36.180 26446-26446 / com.awicompany.forssah   E / UncaughtException:java.lang.NullPointerException:尝试调用   虚方法' android.content.res.Resources   android.content.Context.getResources()'在null对象引用上                                                                                  在android.media.MediaPlayer.create(MediaPlayer.java:1007)                                                                                  在android.media.MediaPlayer.create(MediaPlayer.java:990)                                                                                  在   com.awicompany.forssah.ui.fragments.GetDiscount_Tutorial $ 22.run(GetDiscount_Tutorial.java:2799)                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                  在android.os.Looper.loop(Looper.java:158)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:7229)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)03-09   15:18:36.180 26446-26822 / com.awicompany.forssah I / System.out:   (HTTPLog)-Static:isSBSettingEnabled false 03-09 15:18:36.180   26446-26822 / com.awicompany.forssah I / System.out:(HTTPLog)-Static:   isSBSettingEnabled false 03-09 15:18:36.180   26446-26780 / com.awicompany.forssah D / FA:记录事件(FE):_ ae,   捆绑[{_ o = crash,_sc = HomeActivity,_si = -7309909218007751274,   timestamp = 1489061916190,fatal = 1}] 03-09 15:18:36.200   26446-26780 / com.awicompany.forssah V / FA:使用测量服务   03-09 15:18:36.200 26446-26780 / com.awicompany.forssah V / FA:正在连接   远程服务03-09 15:18:36.430   26446-26446 / com.awicompany.forssah E / AndroidRuntime:FATAL EXCEPTION:   主要                                                                           过程:com.awicompany.forssah,PID:26446                                                                           java.lang.NullPointerException:尝试调用虚方法   ' android.content.res.Resources android.content.Context.getResources()'   在null对象引用上                                                                               在android.media.MediaPlayer.create(MediaPlayer.java:1007)                                                                               在android.media.MediaPlayer.create(MediaPlayer.java:990)                                                                               在   com.awicompany.forssah.ui.fragments.GetDiscount_Tutorial $ 22.run(GetDiscount_Tutorial.java:2799)                                                                               在android.os.Handler.handleCallback(Handler.java:739)                                                                               在android.os.Handler.dispatchMessage(Handler.java:95)                                                                               在android.os.Looper.loop(Looper.java:158)                                                                               在android.app.ActivityThread.main(ActivityThread.java:7229)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

0 个答案:

没有答案