2012-04-20 110 views
0

我在使用共享首选项存储数据时遇到问题。 如果我有以下代码并尝试运行它,它会崩溃。我不知道为什么。共享首选项Android存储数据

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

为什么没有事情发生?这些只是虚拟值,但仍没有任何反应

+0

什么是例外......? – ngesh 2012-04-20 04:23:44

+0

你得到什么类型的错误? – 2012-04-20 04:25:33

+0

我复制了此代码并在我的设备中尝试过。它工作正常。显示所有值..... – 2012-04-20 04:34:33

回答

0

变化

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

SharedPreferences pref = getSharedPreferences("Preference", 
      MODE_WORLD_WRITABLE); 
+0

这将如何?..它只是一个标志,以预防...不决定共享首选项是否可读或可写.. – ngesh 2012-04-20 04:31:34

+0

因为我们使用可读,那么我们如何编写偏好。 – Richa 2012-04-20 04:33:44

0

试试这个,也许是由于空指针exception.I我不知道。

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