Данные общих настроек не сохраняются при передаче строки намерения ... сбой активности - PullRequest
0 голосов
/ 08 ноября 2011

У меня есть 2 мероприятия. В действии A, когда нажатие кнопки отправляет вас в упражнение B, в B оно запрашивает ввод и возвращает этот ввод в A, где оно сохраняется с помощью SharedPreferences. Когда я пытаюсь сохранить это значение с помощью SharedPreferences, мое приложение вылетает. Я не могу понять, что не так. Вот код активности A, который должен сохранять и отображать значение, переданное из операции B.

p.s. использование намерений передать значение обратно из A в B не вызывает проблем, просто сохраняя значение, вызывающее проблему.

TextView teamScore1;
SharedPreferences Score01;
public static String strTScore01 = "MySHaredString";

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

           Score01 = getSharedPreferences(strTScore01, 0);
    String dataReturned01 = Score01.getString("sharedString",
            "Cant LOAD data");
    teamScore1.setText(dataReturned01);
            initialize();
    intents();

}
private void initialize()....
private void intents() {
    if (getIntent().hasExtra("teamScore1")) {

        Intent i = getIntent();
        stringData1 = i.getStringExtra("teamScore1");


        // SCORE VALUES
        if (stringData1.length()> 0) {
            //calculations and data manipulation 
            intTScore_1 = Integer.parseInt(stringData1);
            total = intTScore_1 + total; // add to current total
            stringData1 = Integer.toString(total);


            SharedPreferences.Editor editor = Score01.edit();
            editor.putString("sharedString", stringData1);
            editor.commit();
            Score01 = getSharedPreferences(strTScore01, 0);
            String dataReturned1 = Score01.getString("sharedString",
                    "Cant LOAD data");
            teamScore1.setText(dataReturned1);

LogCat .....

11-07 19:36:01.712: D/AndroidRuntime(905): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<

11-07 19:36:01.712: D/AndroidRuntime(905): CheckJNI is ON

11-07 19:36:01.832: D/AndroidRuntime(905): --- registering native functions ---

11-07 19:36:02.342: D/AndroidRuntime(905): Shutting down VM

11-07 19:36:02.342: D/dalvikvm(905): Debugger has detached; object registry had 1 entries

11-07 19:36:02.372: I/dalvikvm(905): JNI: AttachCurrentThread (from ???.???)

11-07 19:36:02.372: I/AndroidRuntime(905): NOTE: attach of thread 'Binder Thread #3' failed

11-07 19:36:02.732: D/AndroidRuntime(913): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<

11-07 19:36:02.732: D/AndroidRuntime(913): CheckJNI is ON

11-07 19:36:02.852: D/AndroidRuntime(913): --- registering native functions ---

11-07 19:36:03.343: I/ActivityManager(59): Force stopping package com.petermihaylov.android.cardcounter uid=10039

11-07 19:36:03.343: I/Process(59): Sending signal. PID: 898 SIG: 9

11-07 19:36:03.363: I/UsageStats(59): Unexpected resume of com.thenewboston.travis while already resumed in com.petermihaylov.android.cardcounter

11-07 19:36:03.363: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.petermihaylov.android.cardcounter/.MainActivity }

11-07 19:36:03.373: I/WindowManager(59): WIN DEATH: Window{45067ea0 com.petermihaylov.android.cardcounter/com.petermihaylov.android.cardcounter.NewGame paused=false}

11-07 19:36:03.447: D/AndroidRuntime(913): Shutting down VM

11-07 19:36:03.454: D/jdwp(913): Got wake-up signal, bailing out of select

11-07 19:36:03.454: D/dalvikvm(913): Debugger has detached; object registry had 1 entries

11-07 19:36:03.505: I/AndroidRuntime(913): NOTE: attach of thread 'Binder Thread #3' failed

11-07 19:36:03.603: I/ActivityManager(59): Start proc com.petermihaylov.android.cardcounter for activity 
com.petermihaylov.android.cardcounter/.MainActivity: pid=920 uid=10039 gids={}

11-07 19:36:03.663: W/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 898 uid 10039

11-07 19:36:03.873: W/ActivityThread(920): Application com.petermihaylov.android.cardcounter is waiting for the debugger on port 8100...

11-07 19:36:03.913: I/System.out(920): Sending WAIT chunk

11-07 19:36:03.923: I/dalvikvm(920): Debugger is active

11-07 19:36:03.953: I/System.out(920): Debugger has connected

11-07 19:36:03.953: I/System.out(920): waiting for debugger to settle...

11-07 19:36:04.193: I/System.out(920): waiting for debugger to settle...

11-07 19:36:04.393: I/System.out(920): waiting for debugger to settle...

11-07 19:36:04.646: I/System.out(920): waiting for debugger to settle...

11-07 19:36:04.843: I/System.out(920): waiting for debugger to settle...

11-07 19:36:05.057: I/System.out(920): waiting for debugger to settle...

11-07 19:36:05.253: I/System.out(920): waiting for debugger to settle...

11-07 19:36:05.513: I/System.out(920): waiting for debugger to settle...

11-07 19:36:05.746: I/System.out(920): waiting for debugger to settle...

11-07 19:36:05.950: I/System.out(920): waiting for debugger to settle...

11-07 19:36:06.154: I/System.out(920): waiting for debugger to settle...

11-07 19:36:06.358: I/System.out(920): debugger has settled (1438)

11-07 19:36:07.377: I/ActivityManager(59): Displayed activity com.petermihaylov.android.cardcounter/.MainActivity: 3784 ms 
(total 33895 ms)

11-07 19:36:09.143: I/ActivityManager(59): Starting activity: Intent { act=com.petermihaylov.android.NEWGAME cmp=com.petermihaylov.android.cardcounter/.NewGame (has extras) }

11-07 19:36:10.014: I/ActivityManager(59): Displayed activity com.petermihaylov.android.cardcounter/.NewGame: 761 ms (total 761 ms)

11-07 19:36:14.062: I/ActivityManager(59): Starting activity: Intent { act=com.petermihaylov.android.GAME cmp=com.petermihaylov.android.cardcounter/.Game (has extras) }

11-07 19:36:24.117: W/ActivityManager(59): Launch timeout has expired, giving up wake lock!

11-07 19:36:24.186: W/ActivityManager(59): Activity idle timeout for HistoryRecord{45062c78 com.petermihaylov.android.cardcounter/.Game}

11-07 19:36:29.451: W/ActivityManager(59): Activity destroy timeout for HistoryRecord{4505ae90 com.petermihaylov.android.cardcounter/.NewGame}

После нескольких долгих часов попыток поймать утверждения, сужающие проблему, я думаю, что решил ее. Вот код, который, я надеюсь, кому-то поможет

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        initialize();
        intents();

        Score01 = getSharedPreferences(strTScore1, 0);
        String dataReturned1 = Score01.getString("sharedString",
                "Cant LOAD data");
        teamScore1.setText(dataReturned1);
    }

    private void initialize() {...

    private void intents() {
        if (getIntent().hasExtra("teamScore1")) {
            Intent i = getIntent();
            stringData1 = i.getStringExtra("teamScore1");
            Score01 = getSharedPreferences(strTScore1, 0);

            if (stringData1.length() > 0) {


                intTScore_1 = Integer.parseInt(stringData1);
                total = intTScore_1 + total; // add to current total
                stringData1 = Integer.toString(total);

                SharedPreferences.Editor editor = Score01.edit();
                editor.putString("sharedString", stringData1);
                editor.commit();

            }

1 Ответ

0 голосов
/ 12 ноября 2011
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);
        initialize();
        intents();

        Score01 = getSharedPreferences(strTScore1, 0);
        String dataReturned1 = Score01.getString("sharedString",
                "Cant LOAD data");
        teamScore1.setText(dataReturned1);
    }

    private void initialize() {...

    private void intents() {
        if (getIntent().hasExtra("teamScore1")) {
            Intent i = getIntent();
            stringData1 = i.getStringExtra("teamScore1");
            Score01 = getSharedPreferences(strTScore1, 0);

            if (stringData1.length() > 0) {


                intTScore_1 = Integer.parseInt(stringData1);
                total = intTScore_1 + total; // add to current total
                stringData1 = Integer.toString(total);

                SharedPreferences.Editor editor = Score01.edit();
                editor.putString("sharedString", stringData1);
                editor.commit();

            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...