基本上,我有兩個類: - MyActivity.java - OtherClass.java從其他類顯示PopUpWindow - Android電子
概述MyActivity.java的: 沒有在那裏真的很有趣......除了instanciation什麼是OtherClasse.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
*/
}
概述需要:
它有一個可點擊的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:無法添加窗口 - 令牌無效無效;你的活動正在運行?
因爲popup.showAtLocation(tv, Gravity.CENTER_HORIZONTAL, 10, 10);
呼叫至public IBinder getWindowToken()
在電視上......女巫沒有令牌明顯...
你有什麼建議,然後解決呢? 即使使用onLongClick的視圖(View v) 'popup.setContentView(v);'...我仍然有IllegalStateException – 2011-12-19 15:56:38
事實上。我只是試圖在PopUpWindow上顯示一些東西......我也嘗試過使用之前創建的textView,它不起作用...請參閱添加的代碼。 – 2011-12-19 16:15:34
在你更新的代碼中,你將showTextLTV傳遞給父窗口,但電視已經是彈出窗口的子窗口,我將用一段代碼編輯我的答案 – triggs 2011-12-19 20:51:44