popupWindow 弹出另一个PopupWindow出现问题

2014-11-19 00:35
一个popupWindow 里面有个按钮,实现这个按钮的onTouch事件弹出另外一个popupWindow ,这时出现以下错误。


01 06:18:18.214: ERROR/AndroidRuntime(888): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRoot$W@44ef1b68 is not valid; is your activity running?

回复讨论(解决方案)

你贴下代码看
你贴下代码看



关键代码如下:
View functionsView = LayoutInflater.from(context).inflate(R.layout.popup_dianming_btn, null);

functionsView.findViewById(R.id.voiceLL).setOnTouchListener(new       
SoundOnTouchUtilListener(context,data.get(groupPosition), functionsView.findViewById(R.id.voiceLL)));

SoundOnTouchUtilListener如下

public SoundOnTouchUtilListener(Context context,List<Visitor> listCard,View view){
this.listCard=listCard;
this.context=context;
this.view=view;
if(recordingSoundWin==null){
recordingSoundWin=new PopupWindow(LayoutInflater.from(context)
.inflate(R.layout.popupwindow_recordsound, null),
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
ImageView rocketImage = (ImageView)recordingSoundWin.getContentView().findViewById(R.id.rocketImage); 
rocketImage.setBackgroundResource(R.anim.imgchange); 
rocketAnimation=(AnimationDrawable)rocketImage.getBackground();
}
clockLL=(LinearLayout)recordingSoundWin.getContentView().findViewById(R.id.clockLL);
clockTextView=(TextView)recordingSoundWin.getContentView().findViewById(R.id.clockTextView);
}


public boolean onTouch(View v, MotionEvent event) {
//ACTION_MOVE=2, ACTION_UP = 1,ACTION_DOWN = 0;
if(listCard==null||listCard.size()==0){
Toast.makeText(context, R.string.nocards, Toast.LENGTH_SHORT).show();
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
recordingSoundWin.showAtLocation(view, Gravity.CENTER, 0, 0);
startRecording();
break;

default:
break;
}
return false;
}

红色部分为报错地方,求大神给分析一下原因

你贴下代码看



关键代码如下:
View functionsView = LayoutInflater.from(context).inflate(R.layout.popup_dianming_btn, null);

functionsView.findViewById(R.id.voiceLL).setOnTouchListener(new       
SoundOnTouchUtilListener(context,data.get(groupPosition), functionsView.findViewById(R.id.voiceLL)));

SoundOnTouchUtilListener如下

public SoundOnTouchUtilListener(Context context,List<Visitor> listCard,View view){
this.listCard=listCard;
this.context=context;
this.view=view;
if(recordingSoundWin==null){
recordingSoundWin=new PopupWindow(LayoutInflater.from(context)
.inflate(R.layout.popupwindow_recordsound, null),
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
ImageView rocketImage = (ImageView)recordingSoundWin.getContentView().findViewById(R.id.rocketImage); 
rocketImage.setBackgroundResource(R.anim.imgchange); 
rocketAnimation=(AnimationDrawable)rocketImage.getBackground();
}
clockLL=(LinearLayout)recordingSoundWin.getContentView().findViewById(R.id.clockLL);
clockTextView=(TextView)recordingSoundWin.getContentView().findViewById(R.id.clockTextView);
}


public boolean onTouch(View v, MotionEvent event) {
//ACTION_MOVE=2, ACTION_UP = 1,ACTION_DOWN = 0;
if(listCard==null||listCard.size()==0){
Toast.makeText(context, R.string.nocards, Toast.LENGTH_SHORT).show();
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
recordingSoundWin.showAtLocation(view, Gravity.CENTER, 0, 0);
startRecording();
break;

default:
break;
}
return false;
}

红色部分为报错地方,求大神给分析一下原因
没有看到红色


你贴下代码看



关键代码如下:
View functionsView = LayoutInflater.from(context).inflate(R.layout.popup_dianming_btn, null);

functionsView.findViewById(R.id.voiceLL).setOnTouchListener(new       
SoundOnTouchUtilListener(context,data.get(groupPosition), functionsView.findViewById(R.id.voiceLL)));

SoundOnTouchUtilListener如下

public SoundOnTouchUtilListener(Context context,List<Visitor> listCard,View view){
this.listCard=listCard;
this.context=context;
this.view=view;
if(recordingSoundWin==null){
recordingSoundWin=new PopupWindow(LayoutInflater.from(context)
.inflate(R.layout.popupwindow_recordsound, null),
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
ImageView rocketImage = (ImageView)recordingSoundWin.getContentView().findViewById(R.id.rocketImage); 
rocketImage.setBackgroundResource(R.anim.imgchange); 
rocketAnimation=(AnimationDrawable)rocketImage.getBackground();
}
clockLL=(LinearLayout)recordingSoundWin.getContentView().findViewById(R.id.clockLL);
clockTextView=(TextView)recordingSoundWin.getContentView().findViewById(R.id.clockTextView);
}


public boolean onTouch(View v, MotionEvent event) {
//ACTION_MOVE=2, ACTION_UP = 1,ACTION_DOWN = 0;
if(listCard==null||listCard.size()==0){
Toast.makeText(context, R.string.nocards, Toast.LENGTH_SHORT).show();
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
recordingSoundWin.showAtLocation(view, Gravity.CENTER, 0, 0);
startRecording();
break;

default:
break;
}
return false;
}

红色部分为报错地方,求大神给分析一下原因
没有看到红色



recordingSoundWin.showAtLocation(view, Gravity.CENTER, 0, 0); 这句出错,我感觉是View 的问题
你看下新定义一个view=functionsView。findviewbydId,看看出错不
你看下新定义一个view=functionsView。findviewbydId,看看出错不


还是出现相同的错误。
哪位大神帮忙看看~~
我怀疑你这里的context是不是指父类的popwindow而不是指activity,传进来的context改成 XXacitvity.this.getParent()试试
我怀疑你这里的context是不是指父类的popwindow而不是指activity,传进来的context改成 XXacitvity.this.getParent()试试


private DianmingActivity activity;
public SoundOnTouchDianmingListener(Context context,List<Visitor> listCard,View view){
this.listCard=listCard;
this.context=context;
this.view=view;
activity=(DianmingActivity)context;
if(recordingSoundWin==null){
recordingSoundWin=new PopupWindow(LayoutInflater.from(activity.getParent())
.inflate(R.layout.popupwindow_recordsound, null),
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
ImageView rocketImage = (ImageView)recordingSoundWin.getContentView().findViewById(R.id.rocketImage); 
rocketImage.setBackgroundResource(R.anim.imgchange); 
rocketAnimation=(AnimationDrawable)rocketImage.getBackground();
}
clockLL=(LinearLayout)recordingSoundWin.getContentView().findViewById(R.id.clockLL);
clockTextView=(TextView)recordingSoundWin.getContentView().findViewById(R.id.clockTextView);
}
a

我怀疑你这里的context是不是指父类的popwindow而不是指activity,传进来的context改成 XXacitvity.this.getParent()试试


按照您说的我修改代码如上,但是还是出现问题。
a


我怀疑你这里的context是不是指父类的popwindow而不是指activity,传进来的context改成 XXacitvity.this.getParent()试试


按照您说的我修改代码如上,但是还是出现问题。
好吧,那我就没发解决了

a


我怀疑你这里的context是不是指父类的popwindow而不是指activity,传进来的context改成 XXacitvity.this.getParent()试试


按照您说的我修改代码如上,但是还是出现问题。
好吧,那我就没发解决了


popupWindow 弹出另一个PopupWindow这功能是否能实现?您那还有别的方式实现这种效果吗?
楼主解决了嘛……?求赐教!!!