按钮相关问题:尝试在空对象引用

时间:2018-01-31 01:48:50

标签: android

我在Android中遇到此问题。 01-30 20:32:57.415 24227-24227 / edu.gatech.seclass.vcipher E / AndroidRuntime:FATAL EXCEPTION:main                                                                                     处理:edu.gatech.seclass.vcipher,PID:24227                                                                                     java.lang.RuntimeException:无法启动活动ComponentInfo {edu.gatech.seclass.vcipher / edu.gatech.seclass.vcipher.VCipherActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.Button。 nullO对象引用上的setOnClickListener(android.view.View $ OnClickListener)'                                                                                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)                                                                                         在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)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                                      引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'                                                                                         在edu.gatech.seclass.vcipher.VCipherActivity.onCreate(VCipherActivity.java:48)                                                                                         在android.app.Activity.performCreate(Activity.java:6998)                                                                                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)                                                                                         在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)                                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                                         在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 个答案:

答案 0 :(得分:1)

因为你在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);