从onRetainNonConfigurationInstance()获取值时出现空指针异常 - Android

时间:2012-07-15 08:59:53

标签: android

我正在开发一个应用程序,它可以与不同的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 ........

1 个答案:

答案 0 :(得分:0)

您永远不会检查playerhandle是否为null