当前位置: 首页 > 知识库问答 >
问题:

与按钮相关的问题:试图在空对象引用上调用虚拟方法

颛孙凯定
2023-03-14

我在Android中面临这个问题。01-30 20:32:57.415 242 27-24227/edu.gatech.seclass.vcipher e/androidruntime:致命异常:main process:edu.gatech.seclass.vcipher,pid:24227 java.lang.runtimeexception:无法启动活动ComponentInfo{edu.gatech.seclass.vcipher/edu.gatech.seclass.vcipheractivity}:java.lang.nullpointerexception:试图在Android.app.activity的空对象引用上调用虚拟方法'void在Android.app.activitythread.handlelaunchactivity(activitythread.java:2946)在Android.app.activitythread.java:3046)在Android.app.activitythread.-Wrap11(未知源码:0)在Android.app.activitythread.-Wrap11(未知源码:0)在Android.app.activitythread.$h.handleMessage(activitythread.java:1688)在Android.os.handler.dispatchMessage(handler.java:105)在)在com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240)(com.android.internal.os.zygoteinit.main(zygoteinit.java:767)引起的原因是:java.lang.nullpointerException:试图在endu.gatech.seclass.vcipher.vcipherActivity.onCreate(vcipherActivity.java:48)和android.app.activity.performCreate(activity.java:6998)和android.app.instrumentation.callactivityonCreate(在Android.app.activitythread.performLaunchActivity(activitythread.java:2899)在Android.app.activitythread.handleLaunchActivity(activitythread.java:3046) 在Android.app.activitythread.-Wrap11(未知来源:0) 在Android.app.activitythread$h.handleMessage(activitythread.java:1688) 在Android.os.handler.DispatchMessage(Handler.java:105) 在Android.os.looper.loop(looper.java:164) 在Android.app.activitythread.main(activitythread.java:6809) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.zygote$methodandargscaller.run(zygote.java:240) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:767) -------------------------------------------------------------------------------------------------------------------但是我的按钮引用不是空的,但我仍然得到这个问题。

Please help me in resolving this.I am stucked in my android development due to this issue. Not able to resolve it.Help is very much appreciated.

Thanks

共有1个答案

曹铭晨
2023-03-14

因为xml上的id是Android:id=“@+id/button”而不是BTN。
您必须更改

final android.widget.Button button1 = (Button) findViewById(R.id.btn);

final android.widget.Button button1 = (Button) findViewById(R.id.button);
 类似资料: