英文:
java null object reference error / I want my TextView changes color and text when I click the button
问题
Logcat显示第15行有错误。但我不知道原因。:( 我是新手,请温柔地解释给我听。。
下面是整个logcat的内容。
...
2020-10-17 16:50:03.488 9840-9840/? E/city_implement: 在runtime_flags中设置了未知的位:0x8000
2020-10-17 16:50:04.351 9840-9840/? E/AndroidRuntime: 致命异常:主进程中无法启动活动ComponentInfo{com.example.acticity_implements/com.example.acticity_implements.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
at com.example.acticity_implements.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
...
buttonRed的xml代码如下。
英文:
Logcat says 15th line has error. But I don't know the reason. I'm noob so plz explain me softly plz..
Down below is the whole content of logcat.
...
2020-10-17 16:50:03.488 9840-9840/? E/city_implement: Unknown bits set in runtime_flags: 0x8000
2020-10-17 16:50:04.351 9840-9840/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.acticity_implements, PID: 9840
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.acticity_implements/com.example.acticity_implements.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.acticity_implements.MainActivity.onCreate(MainActivity.java:15)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
...
xml code of buttonRed is this.
enter image description here
答案1
得分: 0
这可以非常清楚地分解,从上到下:
- 第15行抛出了一个
NullPointerException
异常,并且有两个引用:buttonRed
和this
。this
永远不可能为空,所以它必须是buttonRed
。 - 在第14行使用了
findViewById
来获取按钮的引用,显然这会返回空。
这是因为您尚未设置布局,因此没有按钮。如果您从 onCreate
中删除所有代码,您将会看到一个没有按钮的白屏。
在 onCreate
中,在 super.onCreate()
之后立即调用 setContentView(R.layout.YOUR_LAYOUT)
。
英文:
This can be broken down quite clear, from top to bottom:
- Line 15 throws a
NullPointerException
and has two references:buttonRed
andthis
.this
cannot be ever null, so it must bebuttonRed
. - On line 14
findViewById
is used to get the reference to the button, this apparently returns null.
This is, because you do not set your layout yet and there is no button. If you remove all code from onCreate
, you will see you get a white screen without buttons.
Call setcontentview(R.layout.YOUR_LAYOUT)
in onCreate
, right after super.onCreate()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论