我的代码中有一个问题,我在片段中有四个值,我需要检查是否至少有一个值不是零。我通过创建方法isEmpty()
来完成此操作。在这种方法中,如果四个值中的至少一个 - 或者两个或三个等 - 是大于零的数字,那么我检索先前设置的共享首选项,否则,所有值都重置为零。
当我调用此方法时,app force将关闭。这是方法:
// All are values are integers, with the exception of counterMeanRepTimeRefined, which is a Float
public void isEmpty() {
if (counterNoSets != 0 || counterRepsPerSet != 0
|| counterMeanRepTimeRefined != 0F || counterSetInterval != 0) {
getSharedPreferenceCounter();
} else {
// Resets all of the four values
counterReset();
}
}
我无法确定问题的原因,但是,相信错误是由isEmpty()中的if语句引起的,特别是检查不等式,当我删除此方法时,检索首选项仍然有效。这是logcat:
07-25 22:17:40.464: E/AndroidRuntime(11773): FATAL EXCEPTION: main
07-25 22:17:40.464: E/AndroidRuntime(11773): Process: com.crackedporcelain.repsetcount, PID: 11773
07-25 22:17:40.464: E/AndroidRuntime(11773): java.lang.NumberFormatException: Invalid int: "Couldn't load saved preference!"
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.Integer.invalidInt(Integer.java:137)
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.Integer.parse(Integer.java:374)
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.Integer.parseInt(Integer.java:365)
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.Integer.parseInt(Integer.java:331)
07-25 22:17:40.464: E/AndroidRuntime(11773): at com.crackedporcelain.repsetcount.CreateFragment.getSharedPreferenceCounter(CreateFragment.java:674)
07-25 22:17:40.464: E/AndroidRuntime(11773): at com.crackedporcelain.repsetcount.CreateFragment.isEmpty(CreateFragment.java:1101)
07-25 22:17:40.464: E/AndroidRuntime(11773): at com.crackedporcelain.repsetcount.CreateFragment.onClick(CreateFragment.java:1181)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.view.View.performClick(View.java:4438)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.view.View$PerformClick.run(View.java:18422)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.os.Handler.handleCallback(Handler.java:733)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.os.Handler.dispatchMessage(Handler.java:95)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.os.Looper.loop(Looper.java:136)
07-25 22:17:40.464: E/AndroidRuntime(11773): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 22:17:40.464: E/AndroidRuntime(11773): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 22:17:40.464: E/AndroidRuntime(11773): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-25 22:17:40.464: E/AndroidRuntime(11773): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-25 22:17:40.464: E/AndroidRuntime(11773): at dalvik.system.NativeStart.main(Native Method)
提前致谢!