سؤال

I have a popupwindow in my application that is meant to act as a sort of settings window. Inside of this there will be a couple little views like checkboxes and what not, but I seem to be having a problem getting one to work properly.

Currently I only have one button in the view that it's inflating, and I'm trying to use it to close the popup window, but I am getting a NullPointerException when I try to setup the onClickListener. In fact, if I refer to the button at all, even in something like "button.isShown()", it gives me the exception as if the button didn't exist.

I've tried everything I could think of, but nothing quite solves this problem. I assume there would be a way to have working buttons inside a PopupWindow, or do I need to modify some form of dialog to get the effect I am after?

Thank you for your help.

هل كانت مفيدة؟

المحلول

Make sure when you call findViewById(R.id.yourButton) that you are using Dialog.findViewById(id) otherwise its going to look on your main layout for the button, and not find it correctly, which will result in NullPointers when you attempt to use the reference.

Edit: here is dome code to make this more clear

    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setView(layout); 
    alertDialog = builder.create();
    alertDialog.show();
    button = (Button) alertDialog.findViewById(R.id.yourButton);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top