【发布时间】:2011-12-19 15:15:55
【问题描述】:
基本上,我有两个课程: - 我的活动.java - 其他类.java
MyActivity.java 概述: 那里没有什么真正有趣的...除了 otherClass.java 需要的实例化
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext() ;
main_activity = this ;
layout = (LinearLayout) findViewById(R.id.layout);
/*
* Do lot of stuff
*/
}
OtherClasse.java 概述:
它有一个可点击的 TextView。当我进行 LongClick 事件时,我想显示一个 PopUpWindow(在 UI 线程上,所以 MyActivity ...)
view.setOnLongClickListener( new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d("TAG", "OnLongClick");
PopupWindow popup = new PopupWindow(activity.getApplicationContext());
//tried with new PopupWindow(MyActivity.context);
popup.setWindowLayoutMode(150, 150);
popup.setContentView( view );
//view corresponds to the TextView.
popup.showAtLocation(MyActivity.layout, Gravity.CENTER_HORIZONTAL, 10, 10);
return true;
}
});
日志表明我进入了onLongClick() ... 但是应用崩溃了……
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
但是 MyActivity.layout 是一个静态的 LinearLayout,所以我可以向它添加视图... 关于如何从其他类的 onClickListener 显示 PopUpWindow 的任何建议?
编辑:
@Override
public boolean onLongClick(View v) {
PopupWindow popup = new PopupWindow(BlaActivity.context);
TextView tv = new TextView(BlaActivity.context);
LayoutParams para = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(para);
tv.setText("My future text ...");
popup.setContentView(tv);
popup.setWidth(400);
popup.setHeight(180);
popup.showAtLocation(tv, Gravity.CENTER_HORIZONTAL, 10, 10);
popup.update();
return true;
}
返回一个
android.view.WindowManager$BadTokenException: 无法添加窗口--token null 无效;您的活动正在进行吗?
因为popup.showAtLocation(tv, Gravity.CENTER_HORIZONTAL, 10, 10);在电视上打电话给public IBinder getWindowToken ()……女巫显然没有令牌……
【问题讨论】:
标签: android onclick popupwindow