0

我正在开发一个应用程序,它使用不同的 Mediaplayer 实例同时播放 2 个音频文件。为了在屏幕旋转时保留媒体播放器的状态,实现了该方法

  public  Object  onRetainNonConfigurationInstance() 
             {
                    HashMap<String,Object> player = new HashMap<String,Object>();
                    MediaPlayer instance = mp;
                    mp = null;
                    MediaPlayer instance1 = mp1;
                    mp1 = null;
                    player.put("mp", instance);
                    player.put("mp1", instance1);
                     return player;

              }

在 onCreate() 中,

    HashMap<String, Object> playerhandle = (HashMap<String, Object>)getLastNonConfigurationInstance();            
                  mp = (MediaPlayer)playerhandle.get("mp");
                      if (mp == null) 
                      {
                         mp = new MediaPlayer();
                       }
                      mp1 = (MediaPlayer)playerhandle.get("mp1");
                      if (mp1 == null) 
                      {
                        mp1 = new MediaPlayer();
                      }

在 logcat 中显示 NullPointerException ............

4

1 回答 1

0

你从不检查是否playerhandlenull

于 2012-07-15T09:03:09.350 回答