user2208748
user2208748

Reputation: 37

onClickListner not working in popUpWindow - null pointer exception

I am trying to implement a popUp window with two buttons. I am able to display the pop up window with buttons successfully. I am trying to implement the button action. But I get null pointer exception.

Here is my code.

View pview =inflater.inflate(R.layout.popupxml(ViewGroup)findViewById(R.layout.activity_event_detail));
popUp = new PopupWindow(pview);
registerBtn = (Button) findViewById(R.id.registerButton);
registerBtn.setOnClickListener(new View.OnClickListener() {

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

            signUp = (Button) findViewById(R.id.signUpExplore);
            notNow = (Button) findViewById(R.id.notNow);

            final OnClickListener notNowClickListner = new OnClickListener() 
              {
                public void onClick(View v) 
                {
                    popUp.dismiss();
                    click = true;
                }
              };


            if (click) {
// I get the null pointer exception in this line.                   
                notNow.setOnClickListener(notNowClickListner);
                popUp.showAtLocation(v, Gravity.CENTER_VERTICAL, 0, 0);
                popUp.update(0, 50, 300, 150);



                click = false;

            } else {
                popUp.dismiss();
                click = true;
            }

        }
    });

Here is the logCAt

04-22 15:42:07.269: E/AndroidRuntime(8575): FATAL EXCEPTION: main
04-22 15:42:07.269: E/AndroidRuntime(8575): java.lang.NullPointerException
04-22 15:42:07.269: E/AndroidRuntime(8575): at com.strive.gostrive.EventDetailActivity$1.onClick(EventDetailActivity.java:153)
04-22 15:42:07.269: E/AndroidRuntime(8575): at   android.view.View.performClick(View.java:2485)
04-22 15:42:07.269: E/AndroidRuntime(8575): at android.view.View$PerformClick.run(View.java:9080)
04-22 15:42:07.269: E/AndroidRuntime(8575): at android.os.Handler.handleCallback(Handler.java:587)
04-22 15:42:07.269: E/AndroidRuntime(8575): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 15:42:07.269: E/AndroidRuntime(8575): at android.os.Looper.loop(Looper.java:130)
04-22 15:42:07.269: E/AndroidRuntime(8575): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-22 15:42:07.269: E/AndroidRuntime(8575): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 15:42:07.269: E/AndroidRuntime(8575): at java.lang.reflect.Method.invoke(Method.java:507)
04-22 15:42:07.269: E/AndroidRuntime(8575): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-22 15:42:07.269: E/AndroidRuntime(8575): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-22 15:42:07.269: E/AndroidRuntime(8575): at dalvik.system.NativeStart.main(Native Method)

I did refer to many examples. But nothing was useful for me.

Need Help!!

Upvotes: 0

Views: 1327

Answers (2)

icyttea
icyttea

Reputation: 1

look at this how to handle onclick event of button inside popup window in android

you need to find the ImageButton in popupwindow like this:

ImageButton imgBtn = (ImageButton)popupView.findViewBy(R.id.imgBtn);

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

I believe you need to initialize your buttons like this

signUp = (Button) popUp.findViewById(R.id.signUpExplore);
notNow = (Button) popUp.findViewById(R.id.notNow);

after calling popUp.setContentView(pview);

you are looking in the wrong place for them so they are returning null

Upvotes: 2

Related Questions