2012-01-10 4 views
0
public class Offer_Popup extends Activity { 


public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.offer_popup); 

     //newly added code, the window popup 
    LayoutInflater inflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     PopupWindow pw = new PopupWindow(
        inflater.inflate(R.layout.popup_example, null, false), 
        100, 
        100, 
        true); 

    pw.showAtLocation(this.findViewById(R.id.relativeLayout_popup), Gravity.CENTER, 0, 0);}  
      ///////////////////// 

popup_example.xml디버그 다음 안드로이드 코드, 팝업 창

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:padding="10dip" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="Test Pop-Up" 
    /> 

</LinearLayout> 

DDMS는 마지막 라인 showAtLocation 오류 보여줍니다.
창을 추가 할 수 없습니다 - 토큰 null이 잘못되었습니다. android.view.WindowManagerBadToken. 팝업을 추가하기 전에 프로그램이 올바르게 실행됩니다. 그러나 팝업 코드가 끝나면 추락했습니다. 무슨 토큰이야?

답변

1

팝업 창을 표시하려면 초기 창을 팝업 할 수 있어야합니다. 하나의 액티비티에는 자동으로 윈도우가 있다고 생각하지만, 처음부터 액티비티는 그렇지 않습니다. 주어진 액티비티에는 onAttachedToWindow()까지 활성 창이 없습니다. 이때 Window가 존재하기 때문에 PopupWindow를 Window에 추가 할 수 있습니다.

코드를 onAttachedToWindow()로 이동하면 코드가 의도 한대로 작동하거나 적어도 다른 오류를 발생시킬 수 있습니다. 이 도움이

희망,

FuzzicalLogic

0

"popup_example"이 (가) 있고 레이아웃 파일에 올바르게 정의되어 있습니까?

파일을 다시 확인하십시오. 그리고 그것을 "팽창시킨"후에 pw의 가치를 재확인하십시오.

+0

PW가 팽창 후 유효 ID = 83xxxxxxxx mWindowManager는 유효한 ID = 83xxxxxxx입니다. – lilzz