2011-12-19 3 views
3

는 기본적으로, 나는이 개 수업을 무엇을하는 것은 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 
    */ 
} 

개요가 필요됩니다의 :

이 클릭 할 수있는 텍스트 뷰가 있습니다. 나는 LongClick 이벤트를 할 때, 나는 (그래서 MyActivity ... UI를 스레드에)

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 (입력을 나타냅니다) ... PopUpWindow을 표시 할 것 그러나 응용 프로그램 충돌 ...

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

는 그러나 정적의 LinearLayout MyActivity.layout, 그래서 에게 ... 그것으로 다른 클래스의 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을 반환 창을 추가 할 수 없습니다 - 토큰 null가 유효하지 않습니다; 당신의 활동은 실행 중입니까? TV에서 public IBinder getWindowToken()-popup.showAtLocation(tv, Gravity.CENTER_HORIZONTAL, 10, 10); 통화가 ... 마녀가 분명 토큰을 ... 없기 때문에

답변

2

는 참조 뷰는

popup.setContentView(view); 

문제가 될 수 여기까지. 매번 팝업 창의 새 인스턴스를 만들지 만 매번 동일한 텍스트 뷰를 사용하는 경우 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 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

당신이 그 문제를 해결하기 위해 다음 무엇을 제안 할 xml 파일? onLongClick (View v) 'popup.setContentView (v);'의 뷰를 사용해도 ... 나는 아직 IllegalStateException을 가지고있다. –

+0

사실. 난 그냥 PopUpWindow에 displaysomething하려고 ... 나는 또한 전에 만든 textView와 노력, 그것은 작동하지 않습니다 ... 코드가 추가 된 참조하십시오. –

+0

업데이트 된 코드에서 showAtLocation의 부모 인 textview tv를 전달했지만 TV는 이미 팝업의 자식이므로 나를 위해 작동하는 코드로 내 대답을 편집합니다 – triggs