我正在尝试在 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)下面是我正在使用的代码: @Overrideprotected 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(); } });}在此先感谢您的帮助!
1 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
你忘了setContentView(R.layout.activity_main);
之后super.onCreate(savedInstanceState);
,如果你还没有在你的活动之前全局声明close
,像这样做:
ImageButton close = (ImageButton) findViewById(R.id.closeDetail);
添加回答
举报
0/150
提交
取消