0

共有設定を消去し続けるアプリケーションを作成しました。SharedPreferences を使用すると、アプリケーションをアンインストールするか、プログラムで設定をクリアするまで保持されることを常に理解していました (私が間違っている可能性があります)。何が起こっているかというと、共有設定の文字列値が 1 つのファイルにあるということです。その値は、if ステートメントを実行する別のファイルで呼び出され、その変数が格納されている場合は、イメージビューを特定のグラフィックに設定します。アプリケーションを閉じて電話の設定で強制終了するか、「高度なタスクキラー」アプリケーションを使用するまで、問題なく動作し、想定どおりにすべてが機能します。共有設定を同じ方法で (if ステートメントを使用せずに) 使用して他のアプリケーションをプログラムしましたが、この問題はないようです。これは、ifステートメントに問題があると私に思わせます。論理的には、何が問題なのかわかりません。どんな助けでも大歓迎です。ファイル 1 コード:

public void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.guessaquaman);

final Button guessbutton = (Button) findViewById(R.id.guess_button);
            guessbutton.setOnClickListener(new View.OnClickListener() {


                public void onClick(View v) {

                    guess=(EditText)findViewById(R.id.guess_edittext);

                    String myguess = guess.getText().toString();
                    String correctanswer = "aquaman";
                    if( myguess.equals( correctanswer ) ){
                        Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_SHORT).show();



                        SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("aquamanticked", "on");
                        editor.commit();

                        Intent myintent1 = new Intent(AquamanGuess.this,PlayActivity.class);
                        startActivity(myintent1);
                        guessbutton.setEnabled(false);
                    }
                }
            });

そして2番目のファイル:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.play);

    SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
    String aquamanticker = sharedPreferences.getString("aquamanticked", "");
    String aman= "on";

    ImageView aquaman = (ImageView) findViewById(R.id.aquaman);
    aquaman.setImageResource(R.drawable.aquaman_small_empty);

    if (aquamanticker == aman ){
    aquaman.setImageResource(R.drawable.aquaman_small_filled); }
4

1 に答える 1

0

2 番目のファイルのその行は見栄えが悪いです。

if (aquamanticker == aman ){

このようにしてみてください:

if (aquamanticker.equalsIgnoreCase(aman) ){

理由:

変数が保持する内容を比較するのではなく、andという2 つのStringオブジェクトを比較しています。aquamantickeramanString

于 2012-07-01T23:53:40.963 に答える