2013-04-17 44 views
0

有人可以请帮忙,我一直在努力处理这段android代码。它不断抛出异常。Android使用SharedPreferences时抛出奇怪的异常

try 
    { 
     SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());//getPreferences(Context.MODE_PRIVATE); 
     String str = sharedPref.getString(MainActivity.APP_DATA_KEY, ""); 
     _appData = FemChoiceAppData.deserialize(str); 

     if (_appData == null) 
      throw new Exception(); 
    } catch (Exception e) 
    { 
     Calendar cl = Calendar.getInstance(TimeZone.getDefault()); 
     int year = cl.get(Calendar.YEAR); 
     int month = cl.get(Calendar.MONTH); 
     int day = cl.get(Calendar.DAY_OF_MONTH); 
     cl.set(year, month, day, 0, 0, 0); 

     _appData = new FemChoiceAppData(); 
     _appData.CurrentMonth = Calendar.getInstance(TimeZone.getDefault()); 
     _appData.CurrentMonth.set(year, month, 1, 0, 0, 0); 

     _appData.CurrentSettings = new FemChoiceSettings(cl.getTime(), 26, 6); 
    } 

    yearTV.setText(_appData.CurrentSettings.getStartDate().getYear()); 
    monthTV.setText(_appData.CurrentSettings.getStartDate().getMonth()); 
    dayTV.setText(_appData.CurrentSettings.getStartDate().getDay()); 

这是种例外,它抛出

04-16 22:54:19.251: E/AndroidRuntime(2063): FATAL EXCEPTION: main 
    04-16 22:54:19.251: E/AndroidRuntime(2063): java.lang.RuntimeException: Unable to start activity ComponentInfo{com../com...Settings}: android.content.res.Resources$NotFoundException: String resource ID #0x71 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.os.Handler.dispatchMessage(Handler.java:99) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.os.Looper.loop(Looper.java:130) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at java.lang.reflect.Method.invokeNative(Native Method) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at java.lang.reflect.Method.invoke(Method.java:507) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at dalvik.system.NativeStart.main(Native Method) 
    04-16 22:54:19.251: E/AndroidRuntime(2063): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x71 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.content.res.Resources.getText(Resources.java:201) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.widget.TextView.setText(TextView.java:2923) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at com...Settings.loadAppData(Settings.java:92) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at com...Settings.setup(Settings.java:64) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at com...Settings.onStart(Settings.java:49) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.Activity.performStart(Activity.java:3791) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1624) 
    04-16 22:54:19.251: E/AndroidRuntime(2063):  ... 11 more 
+0

我认为问题是在加载资源时没有使用共享首选项。你能发布Settings.java的代码(92行)吗? – Kameswari

回答

2

你设置文本为整数。这使得它认为你在你的strings.xml文件中传递了资源id。你想调用yearTV.setText(String.valueOf(_appData.CurrentSettings.getStartDate().getYear()));,所以你传递函数一个字符串,并将其解释为文字。为所有的setText调用做到这一点.e

+0

非常感谢Gabe,你是一个拯救生命的人。这解决了它。 –

相关问题