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

这个报错比较骚,完全只是因为Intent没有写好而已,下面是错误的写法(最好不要在方法外赋值):

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI..._第1张图片

进入本活动时即刻报错:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI..._第2张图片

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

改成下面这样就可以跑了,哪里要用,哪里赋值:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI..._第3张图片

你可能感兴趣的:(java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getI...)