0

有人可以帮忙吗,我一直在努力处理这段 android 代码。它不断抛出异常。

    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
4

1 回答 1

2

您将文本设置为整数。这使它认为你在你的 strings.xml 文件中传递了资源 ID。您想调用yearTV.setText(String.valueOf(_appData.CurrentSettings.getStartDate().getYear()));,因此您将函数传递给字符串并将其解释为文字。对所有 setText 调用执行此操作。e

于 2013-04-17T06:06:49.437 回答