java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI...

weixin_34208283發表於2018-11-11

這個報錯比較騷,完全只是因為Intent沒有寫好而已,下面是錯誤的寫法(最好不要在方法外賦值):

9125154-557c769761d7c272.png

進入本活動時即刻報錯:

9125154-9ff87959310dd6b4.png

11-10 18:23:24.231 17152-17152/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.lwp.justtest, PID: 17152
                                                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lwp.justtest/com.lwp.justtest.next}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference
                                                       at com.lwp.justtest.next.<init>(next.java:17)
                                                       at java.lang.Class.newInstance(Native Method)
                                                       at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

改成下面這樣就可以跑了,哪裡要用,哪裡賦值:

9125154-f1ba59fc50268751.png

相關文章