-1

I have been trying to figure out this problem since yesterday but I am not getting anywhere. The tutorials show the popupwindow dismiss function being called with a button however I have a MenuItem which creates the popup window, inside the window is a cancel button. I want the window to close when the cancel button is pressed. Here is the code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.ResetP:
            passwordWindow();
            break;}

The code for the passwordWindow()

private void passwordWindow()
{
    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.password_layout,null);

    popupWindow = new PopupWindow(container,(int)(width * .6),(int)(height*.3),false);
    popupWindow.showAtLocation(relativeLayout, Gravity.NO_GRAVITY,(int)(width * .2),(int)(height *.3));

    Button btn = (Button) findViewById(R.id.btn_cancel);//This button is in password_layout.xml file
    btn.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View view) {
            popupWindow.dismiss();
        }
    });
}

My Password window has a OK button and a cancel button, I want the window to close when the cancel button is pressed however I get this error

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

This is making me pull my hair out, can someone please help me Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user1281566
  • 133
  • 1
  • 3
  • 15

1 Answers1

3
Button btn = (Button)container.findViewById(R.id.btn_cancel);//This button is in password_layout.xml file

Add container.findViewById...

PriyankaChauhan
  • 953
  • 11
  • 26