0
我工作的彈出窗口上,我有兩個imagebuttons.at開始一切都看起來不錯,但是當我把onclick事件對我的兩個圖像按鈕比我的應用程序崩潰。我不明白爲什麼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)
非常快速和準確的answer..thanks @simple Plan..you救了我的天.. – user3472186
@ user3472186喲歡迎! –