2013-07-22 7 views
6

Viene visualizzato questo errore ogni volta che provo ad avviare la classe della finestra. Sto usando una classe separata, e non solo un metodo all'interno della mia classe di gioco, perché ho bisogno di disabilitare il pulsante indietro su quella finestra popup. Sto chiamando questa classe con un pulsante. Questo codice funziona bene se lo uso nella mia classe di gioco, ma non in una classe separata. Qui è il mio codice:

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() { 

    } 
} 

risposta

13

Non sta chiamando setContentView(R.layout.myLayout) nel metodo onCreate(Bundle). Chiamalo subito dopo super.onCreate(savedInstanceState);.

Questo è dalla pagina delle risorse attività su Android sito sviluppatori:

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).

Edit 1:

Sostituire:

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

con:

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

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

}, 100L); 
+0

Questa è la linea di errore: pwindow.showAtLocation (popupView, Gravity.CENTER, 0, 0); – marjanbaz

+0

@marjanbaz Vedere la mia modifica sopra. – Vikram

+0

Non è un errore omettere una chiamata a 'setContentView'. In questo caso, l'attività avrà solo una vista vuota. Di solito non è utile, ma non impedisce la visualizzazione di una finestra popup. – mrb

Problemi correlati