2011-12-19 82 views
3

基本上,我有兩個類: - 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()在電視上......女巫沒有令牌明顯...

回答

2

你提到的觀點到這裏

popup.setContentView(view); 

可能是問題。每次創建彈出窗口的新實例,但是如果每次都使用相同的textview,那麼會導致IllegalStateException。

下面的代碼只是一個活動和第二個類。 onLongClick創建AnotherClass的一個實例並調用showPopUp。

AnotherClass的構造函數接受上下文作爲參數,稍後將其用於實例化彈出窗口。

showPopUp將視圖用作彈出窗口的父窗口。

活動的onCreate

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button b = (Button)findViewById(R.id.button1); 
     final RelativeLayout parent = (RelativeLayout)findViewById(R.id.layout); 
     b.setOnLongClickListener(new OnLongClickListener() { 

       @Override 
       public boolean onLongClick(View v) { 
        new AnotherClass(getApplicationContext()).showPopUp(parent); 
        return true; 
       } 
      }); 
    } 

第二類

要使用點擊/觸摸監聽器OtherClass你可以宣佈他們爲你通常會卻創造你需要給聽者內彈出活動的背景。像這樣的東西是好的

public class AnotherClass { 
    Context ctx; 
    public AnotherClass(Context ctx){ 
     this.ctx = ctx; 

     //***EXAMPLE*** wont actually be visible as its not added to a view 
     Button b2 = new Button(ctx); 
     b2.setText("show popup"); 
     b2.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 
      showPopUp(v);//View v can be used as the parent 
      return true; 
     } 
    }); 
    } 

    public void showPopUp(View parent) { 

     PopupWindow popup = new PopupWindow(ctx); 
     TextView tv = new TextView(ctx); 
     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(parent, Gravity.CENTER_HORIZONTAL, 10, 10); 
     popup.update(); 
    } 

} 

和XML文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@android:color/black" android:id="@+id/layout"> 
    <Button android:layout_height="wrap_content" 
     android:layout_width="wrap_content" android:id="@+id/button1" 
     android:text="Button" android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" android:layout_marginLeft="51dp" 
     android:layout_marginTop="28dp"></Button> 

</RelativeLayout> 
+0

你有什麼建議,然後解決呢? 即使使用onLongClick的視圖(View v) 'popup.setContentView(v);'...我仍然有IllegalStateException – 2011-12-19 15:56:38

+0

事實上。我只是試圖在PopUpWindow上顯示一些東西......我也嘗試過使用之前創建的textView,它不起作用...請參閱添加的代碼。 – 2011-12-19 16:15:34

+0

在你更新的代碼中,你將showTextLTV傳遞給父窗口,但電視已經是彈出窗口的子窗口,我將用一段代碼編輯我的答案 – triggs 2011-12-19 20:51:44