我正在弹出一个窗口,我有两个imagebuttons.at start all看起来不错,但当我在我的两个图像按钮上放置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)
答案 0 :(得分:2)
你应该改变这个
popcash = (ImageButton)findViewById(R.id.popcash);
使用
popcash = (ImageButton)layout.findViewById(R.id.popcash);
并且也改变了这个
popcoin = (ImageButton) findViewById(R.id.popcoin);
与
popcoin = (ImageButton) layout.findViewById(R.id.popcoin);
这是因为您的两个按钮都来自view layout
inflated
。
答案 1 :(得分:0)
使用此
popcash = (ImageButton )popup.findViewById(R.id.popcash);
popcoin = (ImageButton)popup.findViewById(R.id.popcoin);