java空对象引用错误 / 我希望我的TextView在我点击按钮时更改颜色和文本

huangapple go评论121阅读模式
英文:

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代码如下。

英文:

enter image description here

Logcat says 15th line has error. But I don't know the reason. java空对象引用错误 / 我希望我的TextView在我点击按钮时更改颜色和文本 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

这可以非常清楚地分解,从上到下:

  1. 第15行抛出了一个 NullPointerException 异常,并且有两个引用:buttonRedthisthis 永远不可能为空,所以它必须是 buttonRed
  2. 在第14行使用了 findViewById 来获取按钮的引用,显然这会返回空。

这是因为您尚未设置布局,因此没有按钮。如果您从 onCreate 中删除所有代码,您将会看到一个没有按钮的白屏。

onCreate 中,在 super.onCreate() 之后立即调用 setContentView(R.layout.YOUR_LAYOUT)

英文:

This can be broken down quite clear, from top to bottom:

  1. Line 15 throws a NullPointerException and has two references: buttonRed and this. this cannot be ever null, so it must be buttonRed.
  2. 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()

huangapple
  • 本文由 发表于 2020年10月17日 16:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64400613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定