Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SharedReferences IOException错误_Android_Sharedpreferences - Fatal编程技术网

Android SharedReferences IOException错误

Android SharedReferences IOException错误,android,sharedpreferences,Android,Sharedpreferences,因此,我试图在我的android应用程序中为SharedReferences编写一些字符串。我首先在我的应用程序子类中声明SharedReferences及其原始属性。我希望能够在应用程序的其他活动中更改我的数据值,但我得到以下错误。我该如何解决这个问题?另一个人也提出了同样的问题,但没有人真正回答他,这就是我再次提问的原因。感谢您的帮助!谢谢 SharedPreferencesImpl﹕ writeToFile: Got exception: java.io.IOException: java

因此,我试图在我的android应用程序中为
SharedReferences
编写一些字符串。我首先在我的
应用程序
子类中声明SharedReferences及其原始属性。我希望能够在应用程序的其他活动中更改我的数据值,但我得到以下错误。我该如何解决这个问题?另一个人也提出了同样的问题,但没有人真正回答他,这就是我再次提问的原因。感谢您的帮助!谢谢

SharedPreferencesImpl﹕ writeToFile: Got exception:
java.io.IOException: java.nio.charset.CoderResult[Malformed-input error with erroneous input length 1]
        at com.android.internal.util.FastXmlSerializer.flush(FastXmlSerializer.java:225)
        at com.android.internal.util.FastXmlSerializer.append(FastXmlSerializer.java:86)
        at com.android.internal.util.FastXmlSerializer.escapeAndAppendString(FastXmlSerializer.java:127)
        at com.android.internal.util.FastXmlSerializer.text(FastXmlSerializer.java:361)
        at com.android.internal.util.XmlUtils.writeValueXml(XmlUtils.java:425)
        at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:245)
        at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:185)
        at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:596)
        at android.app.SharedPreferencesImpl.access$800(SharedPreferencesImpl.java:52)
        at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:511)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
一些示例代码: 这在我的
应用程序
类中声明:

editor.putString("firstRun", "true");
editor.apply();
这在我的外部活动中声明:

editor.putString("firstRun", "false");
editor.apply();
这就是我的共享首选项的声明方式(在我的
应用程序
子类中):

编辑: 以下是更多代码: 应用程序类

public class MainApplication extends Application {

    public static SharedPreferences prefs;
    public SharedPreferences.Editor editor;

    public void onCreate() {
        super.onCreate();

        // Data Configurations etc
        prefs = getSharedPreferences("My App", Context.MODE_PRIVATE);
        editor = prefs.edit();
        Map<String, ?> keys = prefs.getAll();
        if (keys.size() == 0) {
            editor.putString("firstRun", "true");
            editor.apply();
        }

        // Block of Code saving more strings to shared preferences
    }
}
应该提到这一点,我还将json内容保存到共享首选项中

共享引用的一些日志猫

Tutorial, true
Notification0, {"alert":"Test Alert","category":"news","date":"Aug 7, 2015","name":"Alert01"}
VersionNum, 1.0
firstRun, false
New0, {"alert":"Test Alert","category":"news","fileData":"<!doctype html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en\"><head><meta content=\"Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.\" name=\"description\"><meta content=\"noodp\" name=\"robots\"><meta content=\"/images/google_favicon_128.png\" itemprop=\"image\"><meta content=\"origin\" id=\"mref\" name=\"referrer\"><title>Google</title>   <script>(function(){window.google={kEI:'29q4Va--H4W0eI2jsMgP',kEXPI:'3700337,3700372,4028875,4029815,4031109,4032235,4032500,4032678,4033307,4033344,4034882,4035869,4036527,4036848,4037333,4037457,4037569,4038012,4038399,4038464,4038821,4039386,4039462,4039879,4039886,4039895,4039937,4040028,4040061,4040112,4040136,4040412,4040513,4040678,4040849,4040865,4040976,4040982,4041079,4041304,4041323,4041440,4042052,4042103,4042125,8300095,8300200,8300203,8500394,8501295,8501407,8501489,8501584,8501767,10200083,10200095,10201249,10201251,16200005,16200009,16200011',authuser:0,j:{en:1,bv:21,pm:'p',u:'c9c918f0',qbp:0,rre:false},kscs:'c9c918f0_21'};google.kHL='en';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute(\"eid\")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute(\"leid\")));)a=a.parentNode;return b};google.https=function(){return\"https:\"==window.location.protocol};google.ml=function(){return null};google.time=function(){return(new Date).getTime()};google.log=function(a,b,d,e,g){a=google.logUrl(a,b,d,e,g);if(\"\"!=a){b=new Image;var c=google.lc,f=google.li;c[f]=b;b.onerror=b.onload=b.onabort=function(){delete c[f]};window.google&&window.google.vel&&window.google.vel.lu&&window.google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,d,e,g){var c=\"\",f=google.ls||\"\";if(!d&&-1==b.search(\"&ei=\")){var h=google.getEI(e),c=\"&ei=\"+h;-1==b.search(\"&lei=\")&&((e=google.getLEI(e))?c+=\"&lei=\"+e:h!=google.kEI&&(c+=\"&lei=\"+google.kEI))}a=d||\"/\"+(g||\"gen_204\")+\"?atyp=i&ct=\"+a+\"&cad=\"+b+c+f+\"&zx=\"+google.time();/^http:/i.test(a)&&google.https()&&(google.ml(Error(\"a\"),!1,{src:a,glmm:1}),a=\"\");return a};google.y={};google.x=function(a,b){google.y[a.id]=[a,b];return!1};google.load=function(a,b,d){google.x({id:a+k++},function(){google.load(a,b,d)})};var k=0;})();google.kCSI={};\ngoogle.j.b=(!!location.hash&&!!location.hash.match('[#&]((q|fp)=|tbs=rimg|tbs=simg|tbs=sbi)'))\n||(google.j.qbp==1);(function(){window.google.sn='webhp';google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},it:{},bfr:!!b,b:{},olh:null};window.performance&&window.performance.now&&(google.timers[a].wsrt=Math.floor(window.performance.now()))};google.tick=function(a,b,d){google.timers[a]||google.startTick(a);google.timers[a].t[b]=d||google.time()};google.bit=function(a,b,d){google.timers[a]||google.startTick(a);var c=google.timers[a].it[b];c||(c=google.timers[a].it[b]=[]);var e=c.push({s:d||google.time()})-1;return function(){c[e]&&(c[e].e=d||google.time())}};google.blockCSI=function(a,b){google.timers[a].b[b]=!0};google.unblockCSI=function(a,b,d){if(a=google.timers[a]){a=a.b;a[b]=!1;for(var c in a)if(a.hasOwnProperty(c)&&a[c])return;google.csiReport&&google.csiReport(void 0,void 0,d)}};google.rolh=function(a){google.uolh();google.timers.load&&(google.timers.load.olh=a,window.addEventListener?window.addEventListener(\"load\",a,!1):window.attachEvent&&window.attachEvent(\"onload\",a))};google.uolh=function(){if(google.timers.load){var a=google.timers.load;a.olh&&(window.addEventListener?window.removeEventListener(\"load\",a.olh,void 0):window.attachEvent&&window.detachEvent(\"onload\",a.olh),a.olh=null)}};google.startTick(\"load\",!0);google.blockCSI(\"load\",\"ol\");google.blockCSI(\"load\",\"xjs\");google.iml=function(a,b){google.tick(\"iml\",a.id||a.src||a.name,b)};})();google.afte=!0;google.aft=functio
Tutorial,true
通知0,{“警报”:“测试警报”,“类别”:“新闻”,“日期”:“2015年8月7日”,“名称”:“警报01”}
VersionNum,1.0
第一轮,错
New0,{“警报”:“测试警报”,“类别”:“新闻”,“文件数据”:“谷歌(函数(){window.Google={kEI:'29q4Va--H4W0eI2jsMgP',kEXPI:'37003370037240288754029815403110940322354032500403267840333040333403440348824035869403274036848403733340374574037569403801240383994036440388214038640394624039886403989540399370404002840414014014014014012404040404040404040404067840408490408650040408760409821079,4041304404132340414404040404202404210340420340430402404210340420321258300900200830020385003948500129550140785014898501584850176710200083102000951020124910201251162000516200009162000011',作者:0,作者:0,作者:0,作者:{en:1,作者:21,作者:pm:'p',作者:u:'c9c918f0',作者:0,作者:false},作者:kscs:'c9c918f0Ø作者:google作者:google作者:google{for(var b;a&(!a.getAttribute | | |)!(b=a.getAttribute(\'eid\”));a=a.parentNode;return b | | google.kEI};google.getLEI=function(a){for(var b=null;a&(!a.getAttribute | | | | | | | |!(b=a.getAttribute(\'leid\”))))a=a.parentNode;return b};google.https=function(){return https=function(){return window https:\\\'https:\\\\\\\\'window https:'';window.location:返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)获取(新的日期)返回(新的日期)返回(新的日期)返回(新的日期)日期)谷歌。谷歌.谷歌.谷歌.谷歌.谷歌.返回(返回(返回(新的)返回)返回(新的图像)若)a(新图像;VAC=新图像;VAC=新形象;var c=c=谷歌;c=谷歌;c=谷歌.谷歌.c=c=c.c.c=谷歌.c.c=谷歌.c.c.c=c.c.c.c.c,c,c,c,c,c,c,c,c,c,c,c,c,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f\“如果(!d&-1==b.搜索(\”ei=\”)若,{var h=google.google.getEI(e)、c=c=c=c=c=c=谷歌.谷歌.谷歌(e)、c=c=谷歌.谷歌.谷歌.f=谷歌.f=谷歌.f=谷歌.谷歌.f=谷歌.谷歌.谷歌.谷歌.谷歌.谷歌(e),f=谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌(e,e,c=c=c=c=c=c=c=c=谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌“+b+c+f+\”&zx=\“+google.time();/^http:/i.test(a)和&google.https()&&(google.ml(错误(\'a'),!1,谷歌.y={;谷歌.x=函数(a,b)a,b)谷歌.谷歌.谷歌.x=函数(a,b)a,b{谷歌.谷歌.y[a.a.id][a,b;b;a,a,glmm:a,glmm:1},glmm:a,glmm:a,glmm:1},a,a,a,a,a,a,1),a,1),a,a,1,1,1,1},1,1},a,a,1,a,1,1,5,1,1,1,1,1,1,1,1,1,谷歌.谷歌.谷歌.装载=a,1,回归,1,1,回归,1,1,1,1,1,1,谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.装载=a,1,1,1,回归,1,1,1,回归,1,1,1,1,1,回归,1,1,1,1,1=simg | tbs=sbi))\n | |(google.j.qbp==1);(函数(){window.google.sn='webhp';google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},it:{},bfr:!!b,b:{},olh:null};window.performance&&window.performance.now&(google.timers[a].wsrt=Math.floor(window.performance.now())};google.tick=function(a,b,b,d){google.timers[a];google.startTick]谷歌.谷歌.谷歌.谷歌.谷歌.时间()的第1244号.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.时间()的时间()的第二个.谷歌.谷歌.谷歌.时间()的时间()()};谷歌.谷歌.谷歌.谷歌.时间()};谷歌.谷歌.谷歌.位位=功能(a,b,b,b,b,d)函数(a)谷歌.谷歌.谷歌.谷歌.计时器[a.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.时间[a.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.时间[a.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.谷歌.时间[a.时间[a.时间.函数(a,b,d){if(a=google.timers[a]){a=a.b;a[b]=!1;for(a中的var c)if(a.hasOwnProperty(c)和&a[c])返回;google.csiReport和&google.csiReport(void 0,void 0,d)};google.rolh=function(a){google.uolh();google.timers.load&(google.timers.load.olh=a,window.addEventListener?window.addEventListener(\'load\\,a,!1):window.attachEvent和window.attachEvent.attachEvent(\'load\\'google\”)函数(google.timers.load){var a=google.timers.load;a.olh&&(window.addEventListener?window.removeEventListener(\'load\',a.olh,void 0):window.attachEvent&&window.detachEvent(\'onload\',a.olh=null)};google.startTick(\'load\',!0);google.blockCSI(\'load\','ol\'ol\');google.blockCSI('load\','oll\,'xjs\');google.iml=tick(',a.id | | a.src | | a.name,b)};};();google.afte=!0;google.aft=functio

奇怪的是,当我稍后在我的应用程序中将Tutorial设置为false时,它确实会将其设置为false。但当我重新运行程序时,Tutorial会设置为true。然而,我不会在任何地方的代码中重置它。firstRun有时也会发生这种情况,但有时它确实设置为false……有什么想法吗?我会仔细检查。

我不知道e您的问题的确切原因,但是
SharedReferences
对象用于存储少量数据。如果您需要存储更多数据,我建议将其保存到内部存储器


我从未在将数据存储和检索到内部存储器时遇到过任何问题,但在
SharedReferences
(例如no
putStringList
)中遇到了bug和令人恼火的限制。对于除了最简单的数据以外的所有数据,我都会避免使用
SharedReferences

您能在代码中显示更多的上下文吗?比如声明、初始化、使用共享的pref和编辑器对象。@ci\u.刚刚添加了更多code@Ryan你能给我们看看你在
SharedReferences
中放的json内容吗?这里似乎有一个狡猾的字符r在那里。@PaulBoddington我会的,但是当我试图重写firstRun字符串时,我得到了上面的错误…所以我怀疑它不仅仅是json的东西。除非它真的是?@Ryan我是afr
public class MainActivity extends Activity {
    public void onCreate() {
        super.onCreate();

        SharedPreferences.Editor editor = MainApplication.prefs.edit();
        if (MainApplication.prefs.getString("firstRun", "false").equals("true")) {
            // Set data correctly
            editor.remove("firstRun");
            editor.putString("firstRun", "false");
            editor.apply();

            // yadda yadda yadda more code doing other things not related to shared preferences
           }
       }
    }
}
Tutorial, true
Notification0, {"alert":"Test Alert","category":"news","date":"Aug 7, 2015","name":"Alert01"}
VersionNum, 1.0
firstRun, false
New0, {"alert":"Test Alert","category":"news","fileData":"<!doctype html><html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en\"><head><meta content=\"Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.\" name=\"description\"><meta content=\"noodp\" name=\"robots\"><meta content=\"/images/google_favicon_128.png\" itemprop=\"image\"><meta content=\"origin\" id=\"mref\" name=\"referrer\"><title>Google</title>   <script>(function(){window.google={kEI:'29q4Va--H4W0eI2jsMgP',kEXPI:'3700337,3700372,4028875,4029815,4031109,4032235,4032500,4032678,4033307,4033344,4034882,4035869,4036527,4036848,4037333,4037457,4037569,4038012,4038399,4038464,4038821,4039386,4039462,4039879,4039886,4039895,4039937,4040028,4040061,4040112,4040136,4040412,4040513,4040678,4040849,4040865,4040976,4040982,4041079,4041304,4041323,4041440,4042052,4042103,4042125,8300095,8300200,8300203,8500394,8501295,8501407,8501489,8501584,8501767,10200083,10200095,10201249,10201251,16200005,16200009,16200011',authuser:0,j:{en:1,bv:21,pm:'p',u:'c9c918f0',qbp:0,rre:false},kscs:'c9c918f0_21'};google.kHL='en';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute(\"eid\")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute(\"leid\")));)a=a.parentNode;return b};google.https=function(){return\"https:\"==window.location.protocol};google.ml=function(){return null};google.time=function(){return(new Date).getTime()};google.log=function(a,b,d,e,g){a=google.logUrl(a,b,d,e,g);if(\"\"!=a){b=new Image;var c=google.lc,f=google.li;c[f]=b;b.onerror=b.onload=b.onabort=function(){delete c[f]};window.google&&window.google.vel&&window.google.vel.lu&&window.google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,d,e,g){var c=\"\",f=google.ls||\"\";if(!d&&-1==b.search(\"&ei=\")){var h=google.getEI(e),c=\"&ei=\"+h;-1==b.search(\"&lei=\")&&((e=google.getLEI(e))?c+=\"&lei=\"+e:h!=google.kEI&&(c+=\"&lei=\"+google.kEI))}a=d||\"/\"+(g||\"gen_204\")+\"?atyp=i&ct=\"+a+\"&cad=\"+b+c+f+\"&zx=\"+google.time();/^http:/i.test(a)&&google.https()&&(google.ml(Error(\"a\"),!1,{src:a,glmm:1}),a=\"\");return a};google.y={};google.x=function(a,b){google.y[a.id]=[a,b];return!1};google.load=function(a,b,d){google.x({id:a+k++},function(){google.load(a,b,d)})};var k=0;})();google.kCSI={};\ngoogle.j.b=(!!location.hash&&!!location.hash.match('[#&]((q|fp)=|tbs=rimg|tbs=simg|tbs=sbi)'))\n||(google.j.qbp==1);(function(){window.google.sn='webhp';google.timers={};google.startTick=function(a,b){google.timers[a]={t:{start:google.time()},it:{},bfr:!!b,b:{},olh:null};window.performance&&window.performance.now&&(google.timers[a].wsrt=Math.floor(window.performance.now()))};google.tick=function(a,b,d){google.timers[a]||google.startTick(a);google.timers[a].t[b]=d||google.time()};google.bit=function(a,b,d){google.timers[a]||google.startTick(a);var c=google.timers[a].it[b];c||(c=google.timers[a].it[b]=[]);var e=c.push({s:d||google.time()})-1;return function(){c[e]&&(c[e].e=d||google.time())}};google.blockCSI=function(a,b){google.timers[a].b[b]=!0};google.unblockCSI=function(a,b,d){if(a=google.timers[a]){a=a.b;a[b]=!1;for(var c in a)if(a.hasOwnProperty(c)&&a[c])return;google.csiReport&&google.csiReport(void 0,void 0,d)}};google.rolh=function(a){google.uolh();google.timers.load&&(google.timers.load.olh=a,window.addEventListener?window.addEventListener(\"load\",a,!1):window.attachEvent&&window.attachEvent(\"onload\",a))};google.uolh=function(){if(google.timers.load){var a=google.timers.load;a.olh&&(window.addEventListener?window.removeEventListener(\"load\",a.olh,void 0):window.attachEvent&&window.detachEvent(\"onload\",a.olh),a.olh=null)}};google.startTick(\"load\",!0);google.blockCSI(\"load\",\"ol\");google.blockCSI(\"load\",\"xjs\");google.iml=function(a,b){google.tick(\"iml\",a.id||a.src||a.name,b)};})();google.afte=!0;google.aft=functio