2013-07-22 87 views
6

我每次嘗試啓動窗口類時都會收到此錯誤。我使用單獨的類,而不僅僅是我的遊戲類中的方法,因爲我需要禁用該彈出窗口上的後退按鈕。我用一個按鈕來調用這個類。如果我在遊戲類中使用它,但不在單獨的類中,此代碼正常工作。這裏是我的代碼:

public class Popup_pogresno extends Activity implements OnClickListener{ 

    private PopupWindow pwindow; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     LayoutInflater layoutInflater 
     = (LayoutInflater)Popup_pogresno.this 
      .getSystemService(LAYOUT_INFLATER_SERVICE); 
     View popupView = layoutInflater.inflate(R.layout.popup, null); 
       pwindow = new PopupWindow(popupView, 300, 170, true); 

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

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      pwindow.dismiss(); 
     }}); 

       pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 

     } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onBackPressed() { 

    } 
} 

回答

13

你是不是在你的onCreate(Bundle)方法調用setContentView(R.layout.myLayout)。在super.onCreate(savedInstanceState);之後立即致電。

這是從Android開發網站上的活動資源頁面:

There are two methods almost all subclasses of Activity will implement:

onCreate(Bundle) is where you initialize your activity. Most importantly, here you will usually call setContentView(int) with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that UI that you need to interact with programmatically.

onPause() is where you deal with the user leaving your activity. Most importantly, any changes made by the user should at this point be committed (usually to the ContentProvider holding the data).

編輯1:

替換:

pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 

有:

new Handler().postDelayed(new Runnable(){ 

    public void run() { 
     pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 
    } 

}, 100L); 
+0

這是錯誤行:pwindow.showAtLocation(popupView,Gravity.CENTER,0,0); – marjanbaz

+0

@marjanbaz看到我上面的編輯。 – Vikram

+0

省略對'setContentView'的調用不是錯誤。在這種情況下,活動只會有一個空白的視圖。這通常不太有用,但它也不會阻止顯示彈出窗口。 – mrb

相關問題