6

My English very very BAD cause i'm Russian. :)

In my application I use SharedPreferences to store my values. The data has stored in the SharedPreferences, when application is running, and after exit from it. And everything works fine until I reboot my device. After reboot I can't use SharedPreferences, and the application doesn't read and write the data from there. I use the function getPreferences(0) to get preferences from application data folder. I also tried to use the getSharedPreferences(myPref, MODE_PRIVATE), but the effect is the same. Saves only one solution - data cleaning application after reboot device.

    favoriteButton = (ImageView) findViewById(R.id.favorite_button);
    SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE);
    if(favorite.getString(""+Loader.currentVideo.getTitle()+"", "") == "true") {
        favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected));
    } else {
        favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty));
    }
    favoriteButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE);
            SharedPreferences.Editor editor = favorite.edit();
            if(favorite.getString(""+Loader.currentVideo.getTitle()+"", "") == "true") {
                favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty));
                Loader.favoriteVideos.remove(Loader.currentVideo);
                editor.remove(""+Loader.currentVideo.getTitle()+"");
            } else {
                favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected));
                Loader.favoriteVideos.add(Loader.currentVideo);
                editor.putString(""+Loader.currentVideo.getTitle()+"", "true");
            }
            editor.commit();
        }
    });
4

3 回答 3

2

谢谢大家的建议!但是我已经解决了这个问题!我希望这对某人有所帮助,这是一个解决方案。

在请求值 SharedPreferences 之前,请检查密钥的可用性!!!

SharedPreferences sharedpreferences = getSharedPreferences("SharedPreferences", MODE_PRIVATE); sharedpreferences.contains("key") --- 检查密钥!

    favoriteButton = (ImageView) findViewById(R.id.favorite_button);
    SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE);
    String tempFav = "";
    if(favorite.contains(""+Loader.currentVideo.getTitle()+"")) {
        tempFav = favorite.getString(""+Loader.currentVideo.getTitle()+"", "");
    }
    if(tempFav.equalsIgnoreCase("true")) {
        favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected));
    } else {
        favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty));
    }
    favoriteButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences favorite = getSharedPreferences("Favorites", MODE_PRIVATE);
            SharedPreferences.Editor editor = favorite.edit();
            String tempCFav = "";
            if(favorite.contains(""+Loader.currentVideo.getTitle()+"")) {
                tempCFav = favorite.getString(""+Loader.currentVideo.getTitle()+"", "");
                Log.d(Loader.currentVideo.getTitle());
                Log.d(tempCFav);
            }
            if(tempCFav.equals("true")) {
                favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_empty));
                Loader.favoriteVideos.remove(Loader.currentVideo);
                editor.remove(""+Loader.currentVideo.getTitle()+"");
            } else {
                favoriteButton.setImageDrawable(getResources().getDrawable(R.drawable.fav_selected));
                Loader.favoriteVideos.add(Loader.currentVideo);
                editor.putString(""+Loader.currentVideo.getTitle()+"", "true");
            }
            editor.commit();
        }
    });
于 2012-08-06T12:14:50.087 回答
0

以下代码应该可以为您保存共享首选项。重要的部分是 edit.commit(); 如果这不起作用,那么可能是您使用的手机有一些不同的行为,因为手机制造商可能已经修改了一些东西。

private static final String PREFERENCES = "Preferences";
static protected SharedPreferences getSharedPreferences( Context context ) {

    return context.getSharedPreferences( PREFERENCES, Context.MODE_PRIVATE);
}

public void setString(String setting, String value) {

    SharedPreferences settings = getSharedPreferences( getApplicationContext() );
    Editor edit = settings.edit();
    edit.putString(setting, value);
    edit.commit();
}
于 2012-08-02T11:55:36.757 回答
0

将这些行添加到清单应用程序标记中。

android:allowBackup="true" android:fullBackupContent="true"

于 2019-03-14T06:58:30.980 回答