0

共有設定を使用してデータを保存するのに問題があります。次のコードを実行しようとすると、クラッシュします。理由はわかりませんが。

public class Favorites extends Activity{

    private static final String TAG_NAME = "title";
    private static final String TAG_URL = "href";


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

        Intent in = getIntent();
        TextView favName = (TextView) findViewById(R.id.textView1);

        String FILENAME = "settings";
        String string = "hello world!";

        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);

        favName.setText(dataFromPrefInt);
}

なぜ何も起こらないのですか?これらは単なるダミー値ですが、それでも何も起こりません

4

2 に答える 2

0

これを試してみてください。nullポインタ例外が原因である可能性があります。よくわかりません。

public class Favorites extends Activity{

    private static final String TAG_NAME = "title";
    private static final String TAG_URL = "href";


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

        Intent in = getIntent();
        TextView favName = (TextView) findViewById(R.id.textView1);

        String FILENAME = "settings";
        String string = "hello world!";

        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", null);
        float dataFromPrefflaot = pref.getFloat("keyFloat", null);
        int dataFromPrefInt = pref.getInt("keyInt", null);
        long dataFromPrefLong = pref.getLong("keyLong", null);
        String dataFromPrefString = pref.getString("keyString", null);

        if(dataFromPrefInt==null)
        {
            favName.setText("");
        }
        else
        {
            favName.setText(dataFromPrefInt);
        }
    }
}
于 2012-04-20T06:23:57.827 に答える
0

変化する

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

  SharedPreferences pref = getSharedPreferences("Preference",
            MODE_WORLD_WRITABLE);
于 2012-04-20T04:28:10.973 に答える