求解!我只要在layout中新建一个布局APP就会闪退,我把那个布局删了就又能运行了这是怎么回事?

E/AndroidRuntime: FATAL EXCEPTION: main

                  Process: com.example.hp.myapp, PID: 17890

                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.myapp/com.example.hp.myapp.UsActivity}: 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:2655)

                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)

                      at android.app.ActivityThread.access$800(ActivityThread.java:180)

                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1562)

                      at android.os.Handler.dispatchMessage(Handler.java:111)

                      at android.os.Looper.loop(Looper.java:194)

                      at android.app.ActivityThread.main(ActivityThread.java:5824)

                      at java.lang.reflect.Method.invoke(Native Method)

                      at java.lang.reflect.Method.invoke(Method.java:372)

                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010)

                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)

                   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.hp.myapp.UsActivity.onCreate(UsActivity.java:38)

                      at android.app.Activity.performCreate(Activity.java:6117)

                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)

                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2608)

                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725) 

                      at android.app.ActivityThread.access$800(ActivityThread.java:180) 

                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1562) 

                      at android.os.Handler.dispatchMessage(Handler.java:111) 

                      at android.os.Looper.loop(Looper.java:194) 

                      at android.app.ActivityThread.main(ActivityThread.java:5824) 

                      at java.lang.reflect.Method.invoke(Native Method) 

                      at java.lang.reflect.Method.invoke(Method.java:372) 

                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010) 

                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 

I/Process: Sending signal. PID: 17890 SIG: 9

Application terminated.


Faith_Insist
浏览 3323回答 3
3回答

GdinKing

你的布局有个Button,你给它setOnClickListener了,但在这之前你没有findViewById

公子小白3474636

.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object referenceButton没有初始化

蜂之谷

空指针呀,没有初始化呗
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android