6

I get this error whenever I try to start my window class. I am using separate class, and not just a method within my game class, cause I need to disable back button on that popup window. I am calling this class with a button. This code works fine if I use it within my game class, but not in separate class. Here is my code:

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

    }
}

1 Answer 1

13

You are not calling setContentView(R.layout.myLayout) in your onCreate(Bundle) method. Call it right after super.onCreate(savedInstanceState);.

This is from the Activity resource page on Android developers site:

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:

Replace:

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

with:

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

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

}, 100L);
Sign up to request clarification or add additional context in comments.

8 Comments

This is the line of error: pwindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
It's not an error to omit a call to setContentView. The Activity will just have an empty view in that case. That's not usually useful, but it also won't prevent a popup window from displaying.
@vikram. Yes, it works now. Sort of. I don't get error any more, and I see the popup, but I don't see main activity in the background, I see white plain screen.
@marjanbaz yes, that's because you probably aren't setting any layout for your activity(or setting an empty layout). I highly recommend you do that. But in any case, what would you like to display behind the popup?
Well, my game activity. This is my popup for wrong answer. After a user press OK, he should go back to the game.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.