1

私は、ユーザーが名前、人種、クラスなどを選択できるこの小さなテキストベースのRPGゲームを作成しています...

ユーザーが使用したい名前を入力し、スピナーからレース、クラスを選択するアクティビティが1つあります。

次に、[キャラクターの作成]ボタンを押すと、すべて次のアクティビティに転送されます。それは完璧に機能します。

問題は、データを保存するときのonPauseイベントにあります。onResume()メソッドが機能しませんか?ビューは空白で表示されます。

選択したコードは次のとおりです。

(これは、データが他のアクティビティから取得される場所です)

String Name = getIntent().getStringExtra("strName");
    textViewStrName.setText(Name);

    String Race = getIntent().getStringExtra("strRace");
    textViewStrRace.setText(Race);

    String Class = getIntent().getStringExtra("strClass");
    textViewStrClass.setText(Class);

(これはonPause()メソッドです)

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.clear();
    String strName = textViewStrName.getText().toString();
    String strRace = textViewStrRace.getText().toString();
    String strClass = textViewStrClass.getText().toString();
    editor.putString("strName", strName);
    editor.putString("strRace", strRace);
    editor.putString("strClass", strClass);
editor.commit();

(これはonResume()メソッドです)

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    TextView textViewStrName = (TextView) findViewById(R.id.textViewStrName);
    TextView textViewStrRace = (TextView) findViewById(R.id.textViewStrRace);
    TextView textViewStrClass = (TextView) findViewById(R.id.TextViewStrClass);
    TextView textViewStrAlliance = (TextView) findViewById(R.id.textViewStrAlliance);


        textViewStrName.setText(preferences.getString("strName", null));
        textViewStrRace.setText(preferences.getString("strRace", null));
        textViewStrClass.setText(preferences.getString("strClass", null));
        textViewStrAlliance.setText(preferences.getString("strAlliance",
                null));

アプリを実行してもエラーは発生しません。

4

1 に答える 1

0

アクティビティ間で SharedPreferences ストレージを使用する場合は、getPreferences の代わりに getSharedPreferences() を使用します。詳細はこちらをご覧ください。

また、変更後のお電話もお忘れなくeditor.commit()

于 2012-10-27T19:50:43.813 に答える