Android Sharedpreference出错了

时间:2014-01-27 10:34:13

标签: java android sharedpreferences

在我的Android应用程序中,我在共享偏好中获取用户的详细信息,并在另一个活动中计算并显示结果...但结果显示错误某些...

我附上以下代码。

MainActivity:

String firstName     = et1.getText().toString();
        String middleName    = et2.getText().toString();
        String lastName      = et3.getText().toString();
        String callFirstName = et4.getText().toString();
        String callMiddleName = et5.getText().toString();
        String callLastName = et6.getText().toString();
        String dates         = et7.getText().toString();
        String months        = et8.getText().toString();
        String years         = et9.getText().toString();

else
        {   

             SharedPreferences setting = this.getSharedPreferences(PREFS_NAME,0);
             SharedPreferences.Editor editors = setting.edit();
             editors.putString("log", "log");


                editors.putString("fn", firstName);
                editors.putString("mn",middleName);
                editors.putString("ln",lastName);
                editors.putString("cfn",callFirstName);
                editors.putString("cmn", callMiddleName);
                editors.putString("cln",callLastName);
                editors.putString("dt",dates);
                editors.putString("mt",months);
                editors.putString("yrs",years);

                editors.commit();



            Intent i = new Intent(MainActivity.this, TabLayoutActivity.class);

tablayoutActivity:

if (setting.getString("log", "").toString().equals("log")) 
       {

            firstN = String.valueOf(setting.getString("fn","not found"));
            middleN= String.valueOf(setting.getString("mn","not found"));
            lastN = String.valueOf(setting.getString("ln","not found"));
            callMN = String.valueOf(setting.getString("cmn","not found"));
            callLN= String.valueOf(setting.getString("cln","not found"));
            callFN = String.valueOf(setting.getString("cfn","not found"));
            daY = String.valueOf(setting.getString("dt","not found"));
            montH= String.valueOf(setting.getString("mt","not found"));
            yeaR = String.valueOf(setting.getString("yrs","not found"));


            int yearat =0;
            int monthat = 0;
            int dayat = 0;

            ch   = 0;
            ch1  = 0;
            ch2  = 0;
            ch3  = 0;
            ch4  = 0;
            pin  = 0;
            pin1 = 0;
            pin2 = 0;
            pin3 = 0;
            pin4 = 0; 

            sum11 = 0;
            sum19 = 0;
            sum18 = 0;
            sum20 = 0;
            sum70 = 0;
            sum80 = 0; 
            sum90 = 0;




            GregorianCalendar gc = new GregorianCalendar();
            yearat = gc.get(Calendar.YEAR);
            yearstr = Integer.toString(yearat);
            monthat = gc.get(Calendar.MONTH) + 1;
            monthstr = Integer.toString(monthat);
            dayat = gc.get(Calendar.DAY_OF_MONTH);
            daystr = Integer.toString(dayat);

            int day1 = Integer.parseInt(daY);
            int month1 = Integer.parseInt(montH);
            int year1 = Integer.parseInt(yeaR);



           String s1 = String.valueOf(TabLayoutActivity.this.getAge(year1, month1, day1));



            sum11 = getSum11((daY) + (montH) + (yeaR));
            sum18 = getSum18((yearstr) + (daY + montH));
            sum19 = getSum19((monthstr) + (sum18));
            sum20 = getSum20((daystr) + sum19);

            sum70 = getsum70(daY);
            sum80 = getSum80(montH);
            sum90 = getSum90(yeaR);


            int val      = 0;
            long val1    = sum11;
            //long val   = sum16;
            val          = Integer.valueOf(s1);
            long val2    = 36 - val1;
            int val4     = 9;           
            long val5    = val2 + val4;
            long val6    = val5 + val4;
            long val7    = val6 + 80;

            if (sum70 > sum80)
            {
                ch1 = (sum70 - sum80);
            }
            else
            {
                ch1 = (sum80 - sum70);
            }

            if (sum70 > sum90)
            {
                ch2 = (sum70 - sum90);
            }
            else
            {
                ch2 = (sum90 - sum70);
            }

            if (sum80 > sum90)
            {
                ch4 = (sum80 - sum90);
            }
            else
            {
                ch4 = (sum90 - sum80);
            }


            if (ch1 > ch2)
            {
                ch3 = ch1 - ch2;
            }
            else
            {
                ch3 = ch2 - ch1;
            }



            if (val <= val2)
            {
                ch = ch1;
            }


            else if ((val > val2) && (val <= val5))
            {
                ch = ch2;
            }

            else if ((val > val2) && (val> val5) && (val <= val6))
            {
                ch = ch3;
            }

            else if ((val > val2) && (val> val5) && (val > val6) && (val<= val7))
            {
                ch = ch4;
            }


            /*if (val <= val2)
            {
                pin = pin1;
            }


            else if ((val > val2) && (val <= val5))
            {
                pin = pin2;
            }

            else if ((val > val2) && (val> val5) && (val <= val6))
            {
                pin = pin3;
            }

            else if ((val > val2) && (val> val5) && (val > val6) && (val<= val7))
            {
                pin = pin4;
            }*/


            pyear            = String.valueOf(sum18);
            pmonth           = String.valueOf(sum19);
            pday             = String.valueOf(sum20);
            chn              = String.valueOf(ch);
            pinn             = String.valueOf(pin);

            yearstring       = yearstr;
            monthstring      = monthstr;
            daystring        = daystr;
            ms               = getCalc(monthstring);




       }

    //calculating single value of year
    private long getSum90(String text) {
        // TODO Auto-generated method stub
        long sum90 = 0;
        char[] name90 = new char[text.length()];
               name90 = text.toCharArray();               
               for(int i=0; i<text.length(); i++)
               {
                   sum90 += value18( name90[i] );
                }
                 //while (sum10>9)               
              while (sum90>9 )
               {                     
                   sum90 = findDigitSum90(sum90);                 
               }
        return sum90;
    }

    private long findDigitSum90(long n) {
        // TODO Auto-generated method stub
        int sum90=0;
        while (n != 0) 
        {
         sum90 += n % 10;
         n = n / 10;
        }
        return sum90;
    }
    //calculate single value month
    private long getSum80(String text) {
        // TODO Auto-generated method stub
        long sum80 = 0;
        char[] name80 = new char[text.length()];
               name80 = text.toCharArray();               
               for(int i=0; i<text.length(); i++)
               {
                   sum80 += value18( name80[i] );
                }
                 //while (sum10>9)               
              while (sum80>9 )
               {             
                   sum80 = findDigitSum80(sum80);                 
               }
        return sum80;
    }

logcat的:

01-27 09:05:08.421: I/Choreographer(5431): Skipped 78 frames!  The application may be doing too much work on its main thread.
01-27 09:05:10.792: D/dalvikvm(5431): GC_CONCURRENT freed 112K, 8% free 2688K/2920K, paused 9ms+432ms, total 1177ms
01-27 09:05:11.771: D/AndroidRuntime(5431): Shutting down VM
01-27 09:05:11.771: W/dalvikvm(5431): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-27 09:05:12.371: E/AndroidRuntime(5431): FATAL EXCEPTION: main
01-27 09:05:12.371: E/AndroidRuntime(5431): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.numero/com.example.numero.TabLayoutActivity}: java.lang.NumberFormatException: Invalid int: "not found"
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.os.Looper.loop(Looper.java:137)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at dalvik.system.NativeStart.main(Native Method)
01-27 09:05:12.371: E/AndroidRuntime(5431): Caused by: java.lang.NumberFormatException: Invalid int: "not found"
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.Integer.invalidInt(Integer.java:138)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.Integer.parse(Integer.java:375)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.Integer.parseInt(Integer.java:366)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at java.lang.Integer.parseInt(Integer.java:332)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at com.example.numero.TabLayoutActivity.onCreate(TabLayoutActivity.java:83)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.Activity.performCreate(Activity.java:5104)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-27 09:05:12.371: E/AndroidRuntime(5431):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-27 09:05:12.371: E/AndroidRuntime(5431):     ... 11 more
01-27 09:10:13.672: I/Process(5431): Sending signal. PID: 5431 SIG: 9

4 个答案:

答案 0 :(得分:4)

问题在于:

daY = String.valueOf(setting.getString("dt","not found"));

然后是:

int day1 = Integer.parseInt(daY);

您无法将“未找到”转换为整数。

编辑:

在进一步使用该函数之前,您可以使用此函数来确定该值是否为String。或者使用此点提供默认值。

http://developer.android.com/reference/android/text/TextUtils.html#isDigitsOnly(java.lang.CharSequence)

答案 1 :(得分:2)

显然问题在于:

 daY = String.valueOf(setting.getString("dt","not found"));
            montH= String.valueOf(setting.getString("mt","not found"));
            yeaR = String.valueOf(setting.getString("yrs","not found"));

您从首选项返回的默认值是“找不到”日,年和月,这是一个字符串,稍后当您从首选项中找不到值时,您尝试将字符串转换为数字。

int year1 = Integer.parseInt(yeaR);

在这种情况下你应该返回一些整数。

setting.getString( “年”, “1980”)

答案 2 :(得分:1)

将{0替换为MODE_PRIVATE

SharedPreferences setting = this.getSharedPreferences(PREFS_NAME,MODE_PRIVATE);

答案 3 :(得分:0)

请检查此行

  String dates = et7.getText().toString();

你给的是什么输入,如果它是

 27/01/2014 or 27-01-2014

问题是/, - 无法解析为整数

下一步:

默认值“”

        daY = String.valueOf(setting.getString("dt",""));
        montH= String.valueOf(setting.getString("mt",""));
        yeaR = String.valueOf(setting.getString("yrs",""));

验证 -

        if(daY.length() > 0)
          int dd = Integer.parseInt(daY);