0

我正在尝试用两个按钮实现一个弹出窗口。我能够成功显示带有按钮的弹出窗口。我正在尝试实现按钮操作。但我得到空指针异常。

这是我的代码。

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;
            }

        }
    });

这是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)

我确实参考了很多例子。但没有什么对我有用。

需要帮忙!!

4

2 回答 2

2

我相信你需要buttons像这样初始化你的

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

调用后popUp.setContentView(pview)

你找错地方了,所以他们回来了null

于 2013-04-22T20:43:53.730 回答
0

看看这个如何处理android弹出窗口内按钮的onclick事件

您需要像这样在弹出窗口中找到 ImageButton:

ImageButton imgBtn = (ImageButton)popupView.findViewBy(R.id.imgBtn);
于 2014-10-23T06:57:27.930 回答