关于空对象引用

时间:2016-12-29 20:24:20

标签: android nullpointerexception android-mediaplayer

我正在制作一个电话拨号应用程序,当您按下按钮时播放声音该应用程序运行良好但是一段时间后我得到一个随机的崩溃,因为准备好的听众我不知道发生了什么,这是错误< / p>

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnPreparedListener(android.media.MediaPlayer$OnPreparedListener)' on a null object reference
                                                                                 at com.alper.pola.andoid.phonedailer.MainActivity$1.onTouch(MainActivity.java:258)

这就是我投射媒体播放器的方式

if (mLastButton == one) {
                               MediaPlayer mediaPlayer ;
                                mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.one);
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mediaPlayer) {
                                        mp1.start();
                                    }
                                });
                                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mediaPlayer) {
                                        mp1.release();
                                    }
                                });

                            }
                            if (mLastButton == two) {
                                MediaPlayer mediaPlayer ;
                                mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.two);
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mediaPlayer) {
                                       mp2.start();
                                    }
                                });
                                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mediaPlayer) {
                                        mp2.release();
                                    }
                                });

                            }
                            if (mLastButton == three) {
                                MediaPlayer mediaPlayer ;

                                mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.three);
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mediaPlayer) {
                                        mp3.start();
                                    }
                                });
                                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mediaPlayer) {
                                        mp3.release();
                                    }
                                });

                            }
                            if (mLastButton == four) {
                                MediaPlayer mediaPlayer ;
                                mediaPlayer = MediaPlayer.create(MainActivity.this,R.raw.four);
                                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                    @Override
                                    public void onPrepared(MediaPlayer mediaPlayer) {
                                        mp4.start();
                                    }
                                });
                                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                    @Override
                                    public void onCompletion(MediaPlayer mediaPlayer) {
                                        mp4.release();
                                    }
                                });
                            }

应用程序工作正常,当我点击它时相同的按钮正在工作但是在按钮停止工作之后我用每个按钮得到这个崩溃它开始工作正常但是在我输入几个数字之后应用程序崩溃< / p>

1 个答案:

答案 0 :(得分:0)

在处理MediaPlayer时,您使用的效率非常低。我仍然可以帮助您解决此代码的问题。使用mediaPlayer.prepare()发起呼叫。查看此代码:

if (mLastButton == one) {

     mp1 = MediaPlayer.create(MainActivity.this,R.raw.one);
     mp1.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
         @Override
         public void onPrepared(MediaPlayer mediaPlayer) {
             mp1.start();
     });
     mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
           @Override
           public void onCompletion(MediaPlayer mediaPlayer) {
             mp1.release();
           }
     });
     mp1.prepare();

}

对其他人也这样做。有更好的方法可以做到这一点,但与你的代码一样,这是最短的。