Unable to add window -- token null is not valid; is your activity running?

在使用PopupWindow时出现的error,这个错误有一句话is your activity running?看到这句话时,立刻想到了是由于PopupWindow依赖的窗口没销毁掉。
PopupWindow的创建方式:

// 获取自定义布局文件activity_popupwindow_left.xml的视图  
private void createPop(){
 View popupWindow_view =getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null, false);  
  // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度  
      popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);  
  // 设置动画效果  
      popupWindow.setAnimationStyle(R.style.AnimationFade); 
      popupWindow.showAtLocation(mBmiValue , Gravity.BOTTOM, 0, 0);//mBmivalue是锚点
  } 

所以就立刻想到了在onDestory()方法中调用popupWindow.dismiss(),结果不行。

源码是这样写的(写了部分源码):

public class ChatRoomActivity extends FragmentActivity

    private Button mBmiValue;
    private PopupWindow popupWindow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
          setContentView(R.layout.activity_chatroom);
          mBmiValue = (TextView) findViewById(R.id.tvBmiValue);
          //调用上面的创建pop函数
          createPop();
    }


private void createPop(){
      View  popupWindow_view=getLayoutInflater().
                         inflate(R.layout.activity_popupwindow_left, null, false); 

  // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度  
      popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);  
  // 设置动画效果  
      popupWindow.setAnimationStyle(R.style.AnimationFade); 
      popupWindow.showAtLocation(mBmiValue , Gravity.BOTTOM, 0, 0);//mBmivalue是锚点
  } 
    @Override
    public void onDestroy() {
        popupWindow.dismiss();
        super.onDestroy();

    }
 }

这要写运行结果是第一次进入这个ChatRoomActivity 时,popupWindow显示正常,立刻退出,在进入,呵呵,报错了Unable to add window – token null is not valid; is your activity running?,立即在onDestroy()加了 popupWindow.dismiss();认为这样就可以了。

重复进入activity,在立刻退出,结果还是报错,最后在这个http://stackoverflow.com/questions/4187673/problems-creating-a-popup-window-in-android-activity里面找到了解决方法。
为什么会出现这个原因呢?是因为popupWindow显示依赖activity,并且要等activity所有的生命周期方法全部执行完成才能显示。所以修改了代码

public class ChatRoomActivity extends FragmentActivity

    private Button mBmiValue;
    private PopupWindow popupWindow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
          setContentView(R.layout.activity_chatroom);
          mBmiValue = (TextView) findViewById(R.id.tvBmiValue);
          mBmiValue.post(new Runnable(){
          //调用上面的创建pop函数
          createPop();
          });
        /*  //调用上面的创建pop函数
          createPop();
        */
    }


private void createPop(){
      View  popupWindow_view=getLayoutInflater().
                         inflate(R.layout.activity_popupwindow_left, null, false); 

  // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度  
      popupWindow = new PopupWindow(popupWindow_view, 200, LayoutParams.MATCH_PARENT, true);  
  // 设置动画效果  
      popupWindow.setAnimationStyle(R.style.AnimationFade); 
      popupWindow.showAtLocation(mBmiValue , Gravity.BOTTOM, 0, 0);//mBmivalue是锚点
  } 
    @Override
    public void onDestroy() {
        popupWindow.dismiss();
        super.onDestroy();

    }
 }

加入了 mBmiValue.post(new Runnable(){
//调用上面的创建pop函数
createPop();
});在运行时没有报错了,post()可以延迟到所有生命周期方法执行完后,在显示popupWindow

你可能感兴趣的:(Android开发记录)