添加SharedPreference write

时间:2019-02-27 13:44:14

标签: android nullpointerexception

感谢这个网站,我的应用程序进展顺利。但是,当切换到我刚刚创建的新活动时,它会崩溃。用户有6个活动可以回答问题,在活动#5中,当我按继续按钮时,应用程序崩溃。我得到的标准是“不幸的是,应用程序已停止。”当我按OK时,该应用程序将关闭并重新打开,然后返回到活动4,该活动将保留保存到SharedPreferences中的用户输入。

从我按下继续按钮开始记录:

02-27 08:42:54.231 7307-7307/com.health.hermes D/ViewRootImpl: MSG_RESIZED: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
02-27 08:42:54.541 7307-7307/com.health.hermes D/ViewRootImpl: ViewPostImeInputStage processPointer 0
02-27 08:42:54.611 7307-7307/com.health.hermes D/ViewRootImpl: ViewPostImeInputStage processPointer 1
02-27 08:42:54.621 7307-7307/com.health.hermes I/Timeline: Timeline: Activity_launch_request id:com.health.hermes time:1000022770
02-27 08:42:54.681 7307-7307/com.health.hermes D/TextView: setTypeface with style : 0
02-27 08:42:54.681 7307-7307/com.health.hermes D/TextView: setTypeface with style : 0
02-27 08:42:54.691 7307-7307/com.health.hermes D/AndroidRuntime: Shutting down VM
02-27 08:42:54.691 7307-7307/com.health.hermes E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.health.hermes, PID: 7307
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.health.hermes/com.health.hermes.KiPercentActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setFilters(android.text.InputFilter[])' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
        at android.app.ActivityThread.access$1100(ActivityThread.java:221)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setFilters(android.text.InputFilter[])' on a null object reference
        at com.health.hermes.KiPercentActivity.onCreate(KiPercentActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:6876)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:158) 
        at android.app.ActivityThread.main(ActivityThread.java:7225) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

如果您找到原因,请指出如何更好地理解logcat,以便开始诊断自己。谢谢!

1 个答案:

答案 0 :(得分:0)

这是你的答案。

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.widget.EditText.setFilters(android.text.InputFilter [])”         在com.health.hermes.KiPercentActivity.onCreate(KiPercentActivity.java:30)         在android.app.Activity.performCreate(Activity.java:6876)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)

您的EditText为空,这就是为什么。

更新 最终有多个项目为空,谢谢您抽出宝贵的时间查看我的日志。答案已经在这里覆盖:

What is a NullPointerException, and how do I fix it?