2011-12-26 47 views
0

このコードを実行すると、プログラムが強制終了されます。誰でも私に教えてください。解決策を教えてください。 package com.test.sharedPreferences;共有プリファレンス強制終了

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Sharedpreference extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     SharedPreferences pref = getSharedPreferences("Preference",MODE_WORLD_READABLE); 
     SharedPreferences.Editor editor = pref.edit(); 
     editor.putBoolean("keyBoolean", true); 
     editor.putFloat("keyFloat", 1.0f); 
     editor.putInt("keyInt", 1); 
     editor.putLong("keyLong", 1000000L); 
     editor.putString("keyString", "Hello Android"); 
     editor.commit(); 

// boolean dataFromPrefBool = pref.getBoolean("keyBoolean", false); 
// float dataFromPrefflaot = pref.getFloat("keyFloat", 0.0f); 
     int dataFromPrefInt = pref.getInt("keyInt", 0); 
// long dataFromPrefLong = pref.getLong("keyLong", 0); 
// String dataFromPrefString = pref.getString("keyString", null); 

     TextView tv = new TextView(this); 
     tv.setText(dataFromPrefInt); 
     setContentView(tv); 

    } 
    } 
+3

logcatエラーを貼り付けます。 – aromero

+0

例外は何ですか? logcat出力pls –

+0

あなたの好みの名前と静的な文字列の値は宣言にあります。 –

答えて

0

あなたがのgetIntを書くとき、私はContext.MODE_PRIVATE

+0

コードにエラーはありませんでした.iそのコードを別のプロジェクトに貼り付けました。 –

0

を使用します....あなたはエディタを開いていることを意味します。

まず、エディタを開き、getIntコードを書き込んでください。

SharedPreferences pref = getSharedPreferences("Preference",MODE_WORLD_READABLE); 
int dataFromPrefInt = pref.getInt("keyInt", 0); 
+1

値を読み取るエディタは必要ありません。 –

+0

私はエディタについて書いたことを申し訳ありません。しかし、貼り付けたコードは正常に動作しています。だからそれを試してみてください。 –

0

ここに問題があります。あなたはMODE_WORLD_READABLEを使用して共有設定を開いている、ここで(すなわち、読み取り専用モード):

SharedPreferences.Editor editor = pref.edit(); 
editor.putBoolean("keyBoolean", true); 
... 
editor.commit(); 

のdoesnの」:

SharedPreferences pref = getSharedPreferences("Preference",MODE_WORLD_READABLE); 

してから、ここでの共有設定を編集しようとすることにより、そのフォローアップそれについての何かが間違っているように見えますか?そうでない場合は、here's the documentation to clarify things.

関連する問題