我正在处理一个弹出窗口,我有两个图像按钮。开始时一切看起来都不错,但是当我在两个图像按钮上放置 onclick 事件时,我的应用程序崩溃了。我不明白为什么 onclick 会出现错误。以下是我的代码:
protected void popup() {
RelativeLayout viewGroup(RelativeLayout)MainActivity.this.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
final PopupWindow popup = new PopupWindow(this);
popup.setContentView(layout);
popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(null);
popup.setFocusable(true);
popup.showAtLocation(layout, Gravity.CENTER, 1, 1);
popcash = (ImageButton )findViewById(R.id.popcash);
popcash.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "on click image button", Toast.LENGTH_LONG).show();
}
});
popcoin = (ImageButton) findViewById(R.id.popcoin);
popcoin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("hi", "there");
}
});
}
但我收到以下错误
04-25 17:18:13.856: E/AndroidRuntime(1521): java.lang.NullPointerException
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.example.scratchcarddemo.MainActivity.popup(MainActivity.java:304)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.example.scratchcarddemo.MainActivity$2.onTouch(MainActivity.java:225)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.View.dispatchTouchEvent(View.java:3762)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.os.Looper.loop(Looper.java:123)
04-25 17:18:13.856: E/AndroidRuntime(1521): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 17:18:13.856: E/AndroidRuntime(1521): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:18:13.856: E/AndroidRuntime(1521): at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 17:18:13.856: E/AndroidRuntime(1521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 17:18:13.856: E/AndroidRuntime(1521): at dalvik.system.NativeStart.main(Native Method)