我正在尝试在 ImageButton 上设置 onClickListener,但出现以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bordi.hotel, PID: 18420
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bordi.hotel/com.example.bordi.hotel.DetaiImage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
下面是我正在使用的代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
close = (ImageButton) findViewById(R.id.closeDetail);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DetaiImage.super.onBackPressed();
}
});
}
在此先感谢您的帮助!
慕桂英546537
相关分类