android studio 运行活动闪退Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'vo

2020年5月4日 42点热度 0条评论 来源: sf9090

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.mylogin0310.LoginActivity.onCreate(LoginActivity.java:75)

问题描述:FATAL EXCEPTION: main

Process: com.example.mylogin0310, PID: 19367
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mylogin0310/com.example.mylogin0310.LoginActivity}: 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:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access 800 ( A c t i v i t y T h r e a d . j a v a : 167 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d 800(ActivityThread.java:167) at android.app.ActivityThread 800(ActivityThread.java:167)atandroid.app.ActivityThreadH.handleMessage(ActivityThread.java:1419)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit M e t h o d A n d A r g s C a l l e r . r u n ( Z y g o t e I n i t . j a v a : 964 ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e I n i t . m a i n ( Z y g o t e I n i t . j a v a : 759 ) C a u s e d b y : j a v a . l a n g . N u l l P o i n t e r E x c e p t i o n : A t t e m p t t o i n v o k e v i r t u a l m e t h o d ′ v o i d a n d r o i d . w i d g e t . B u t t o n . s e t O n C l i c k L i s t e n e r ( a n d r o i d . v i e w . V i e w MethodAndArgsCaller.run(ZygoteInit.java:964) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View MethodAndArgsCaller.run(ZygoteInit.java:964)atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)Causedby:java.lang.NullPointerException:Attempttoinvokevirtualmethodvoidandroid.widget.Button.setOnClickListener(android.view.ViewOnClickListener)’ on a null object reference

笔者再进行修改主页面活动时出现的问题,我新建个mainActivity,将原来的主活动复制到了loginActivity中,结果导致跳转loginActivity时APP闪退。

原因:闪退一定是有问题,有可能是背景图格式不对,或者背景图过大,但是明显我这个是代码问题。

如下图,我忘记修改了 .mxl了

总结,学习过程一定要严谨,有错误仔细读取下方的debug、error。

    原文作者:sf9090
    原文地址: https://blog.csdn.net/sf9090/article/details/105923980
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。