我正在尝试用两个按钮实现一个popUp窗口。我能够成功地显示带有按钮的弹出窗口。我正在尝试实现按钮操作。但我得到空指针异常。
这是我的代码。
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)
我确实参考了很多例子。但没有什么对我有用。
需要帮助!!
答案 0 :(得分:1)
我相信你需要像这样初始化你的buttons
signUp = (Button) popUp.findViewById(R.id.signUpExplore);
notNow = (Button) popUp.findViewById(R.id.notNow);
致电popUp.setContentView(pview)
;
你正在寻找错误的地方,以便他们返回null
答案 1 :(得分:0)
查看此how to handle onclick event of button inside popup window in android
你需要在popupwindow中找到像这样的ImageButton:
ImageButton imgBtn = (ImageButton)popupView.findViewBy(R.id.imgBtn);