0

enter image description here

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 Answers1

0

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()

Michiel
  • 767
  • 4
  • 19
  • My English is bad so I don't know How to express my thankful mind fully. I really really appreciate with your kind and helpful answer. Thank you so much!! :D – Rabbit_KIm Oct 17 '20 at 10:31