本文介绍了重启设备后,Android共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的英语非常非常糟糕的,因为我是俄罗斯人。 :)

在我的应用程序使用共享preferences 来存储我的价值观。
该数据已存储在共享preferences ,当应用程序正在运行,之后从中退出。而一切,直到我重新启动我的设备工作正常。重新启动后,我不能用共享preferences ,并且应用程序不会从那里读取和写入数据。
我用函数的get preferences(0)来获得$ P $从应用程序数据文件夹pferences。
我也试着使用 getShared preferences(我的preF,MODE_PRIVATE),但效果是一样的。仅保存一个解决方案 - 数据重启设备后清洗应用

  favoriteButton =(ImageView的)findViewById(R.id.favorite_button);
    共享preferences喜爱= getShared preferences(最爱,MODE_PRIVATE);
    如果(favorite.getString(+ Loader.currentVideo.getTitle()+,)==真){
        favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_selected));
    }其他{
        favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_empty));
    }
    favoriteButton.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            共享preferences喜爱= getShared preferences(最爱,MODE_PRIVATE);
            共享preferences.Editor编辑= favorite.edit();
            如果(favorite.getString(+ Loader.currentVideo.getTitle()+,)==真){
                favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_empty));
                Loader.favoriteVideos.remove(Loader.currentVideo);
                editor.remove(+ Loader.currentVideo.getTitle()+);
            }其他{
                favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_selected));
                Loader.favoriteVideos.add(Loader.currentVideo);
                editor.putString(+ Loader.currentVideo.getTitle()+,真);
            }
            editor.commit();
        }
    });


解决方案

感谢大家的意见!但我已经解决了这个问题!
我希望这可以帮助别人,这里有一个解决方案。

您请求的值共享preferences之前,请检查按键的可用性!

共享preferences共享preferences = getShared preferences(共享preferences,MODE_PRIVATE);
共享preferences.contains(钥匙) ---检查关键!

  favoriteButton =(ImageView的)findViewById(R.id.favorite_button);
    共享preferences喜爱= getShared preferences(最爱,MODE_PRIVATE);
    串tempFav =;
    如果(favorite.contains(+ Loader.currentVideo.getTitle()+)){
        tempFav = favorite.getString(+ Loader.currentVideo.getTitle()+,);
    }
    如果(tempFav.equalsIgnoreCase(真)){
        favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_selected));
    }其他{
        favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_empty));
    }
    favoriteButton.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            共享preferences喜爱= getShared preferences(最爱,MODE_PRIVATE);
            共享preferences.Editor编辑= favorite.edit();
            串tempCFav =;
            如果(favorite.contains(+ Loader.currentVideo.getTitle()+)){
                tempCFav = favorite.getString(+ Loader.currentVideo.getTitle()+,);
                Log.d(Loader.currentVideo.getTitle());
                Log.d(tempCFav);
            }
            如果(tempCFav.equals(真)){
                favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_empty));
                Loader.favoriteVideos.remove(Loader.currentVideo);
                editor.remove(+ Loader.currentVideo.getTitle()+);
            }其他{
                favoriteButton.setImageDrawable(getResources()getDrawable(R.drawable.fav_selected));
                Loader.favoriteVideos.add(Loader.currentVideo);
                editor.putString(+ Loader.currentVideo.getTitle()+,真);
            }
            editor.commit();
        }
    });

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();
        }
    });
解决方案

Thank you all for your advice! But I had solved this problem!I hope this helps someone, here's a solution.

Before you request a value SharedPreferences, check the availability of keys!!!

SharedPreferences sharedpreferences = getSharedPreferences("SharedPreferences", MODE_PRIVATE);sharedpreferences.contains("key") --- check 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();
        }
    });

这篇关于重启设备后,Android共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 18:10