1

私はこれに対する答えを探していましたが、ほとんどが getApplicationContext() の問題を指摘しているようですが、私は getApplicationContext() を使用していません。基本的に、アクティビティの起動時にポップアップウィンドウを開こうとしていますが、アクティビティが開始されると、上記のエラーで強制的に閉じられます。

私が書いたコードは次のとおりです。

protected void onCreate(Bundle paramBundle) {
    super.onCreate(paramBundle);
    setContentView(R.layout.installguides_menu);
    setTitleFromActivityLabel(R.id.title_text);

    btn_Back = (Button) findViewById(R.id.btn_Back);
    btn_Back.setOnClickListener (btn_Back_onClick);

    btn_Ubuntu10Guide   = (Button) findViewById(R.id.btn_Ubuntu10Guide);
    btn_Ubuntu12Guide   = (Button) findViewById(R.id.btn_Ubuntu12Guide);
    btn_BacktrackGuide  = (Button) findViewById(R.id.btn_BacktrackGuide);
    btn_DebianGuide     = (Button) findViewById(R.id.btn_DebianGuide);

    btn_Ubuntu10Guide   .setOnClickListener (btn_Ubuntu10Guide_onClick);
    btn_Ubuntu12Guide   .setOnClickListener (btn_Ubuntu12Guide_onClick);
    btn_BacktrackGuide  .setOnClickListener (btn_BacktrackGuide_onClick);
    btn_DebianGuide     .setOnClickListener (btn_DebianGuide_onClick);

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.donation_popup, null);  
             final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT);  

     Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
     btnDismiss.setOnClickListener(new Button.OnClickListener(){

     public void onClick(View v) {
      popupWindow.dismiss();
     }});

     popupWindow.showAsDropDown(btn_Ubuntu10Guide, 50, -30);
}

あなたたちが助けてくれることを願っています。何時間も答えを探していたので、私の最後の希望です!

4

1 に答える 1

1

私の最初の推測では、 内にポップアップを表示することは許可されていませんonCreate。アプリケーションを初期化してレイアウトを構築するためのものですが、代わりにポップアップを表示するように強制しています。

onStart()それを示すのに適した場所です。この関数をオーバーライドして、そこにポップアップを表示します。onResume() で実行したい場合は、ユーザーがアプリケーションを起動したときだけでなく、別のアクティビティに移動した後に戻ってきたときにも表示されることに注意してください。

EDIT 2: onStart または onCreate でポップアップを作成するには、これを試してください:

       LayoutInflater inflater = (LayoutInflater)
               this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            PopupWindow pw = new PopupWindow(
               inflater.inflate(findViewById(R.id.popup_layout), null, false), 
               100, 
               100, 
               true);

// delaying popup until after all application initialization is done
    findViewById(R.id.main_page_layout).post(new Runnable() {
       public void run() {
         pw.showAtLocation(findViewById(R.id.main_page_layout), Gravity.CENTER, 0, 0);
       }
    });

を使用してアクティビティとポップアップ レイアウトに ID を追加します

android:id="@+id/main_page_layout"

android:id="@+id/popup_layout"

それぞれ、上記のコードで使用する ID と一致するようにします。

于 2012-07-15T23:09:05.173 に答える