title: Android SharedPreferences IOException Error
body: <p>So I'm trying to write some strings to <code>SharedPreferences</code> in my android app. I first declare SharedPreferences and its original properties in my <code>Application</code> subclass. I want to be able to change the values of my data in other activities in the app but I get the error below. How should I fix this? Some other guy posted the same question but no one really answered him so that's why I'm asking again. Any help is appreciated! Thanks </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Some example code:
  This is declared in my <code>Application</code> class:</p>
  
  <pre><code>editor.putString("firstRun", "true");
  editor.apply();
  </code></pre>
  
  <p>This is declared in my outside activity:</p>
  
  <pre><code>editor.putString("firstRun", "false");
  editor.apply();
  </code></pre>
  
  <p>And this is how my shared preferences is declared (in my <code>Application</code> subclass):</p>
  
  <pre><code>SharedPreferences prefs = getApplicationContext().getSharedPreferences("My App", 0);
  </code></pre>
  
  <p>EDIT: 
  Here's more code:
  Application Class</p>
  
  <pre><code>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&lt;String, ?&gt; keys = prefs.getAll();
          if (keys.size() == 0) {
              editor.putString("firstRun", "true");
              editor.apply();
          }
  
          // Block of Code saving more strings to shared preferences
      }
  }
  </code></pre>
  
  <p>Activity where preferences are being changed:</p>
  
  <pre><code>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
             }
         }
      }
  }
  </code></pre>
  
  <p>SHOULD'VE MENTIONED THIS, I am also saving json stuff to the shared preferences.</p>
  
  <p>Some logcats of the <code>SharedPreferences</code>:</p>
  
  <pre><code>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":"&lt;!doctype html&gt;&lt;html itemscope=\"\" itemtype=\"http://schema.org/WebPage\" lang=\"en\"&gt;&lt;head&gt;&lt;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\"&gt;&lt;meta content=\"noodp\" name=\"robots\"&gt;&lt;meta content=\"/images/google_favicon_128.png\" itemprop=\"image\"&gt;&lt;meta content=\"origin\" id=\"mref\" name=\"referrer\"&gt;&lt;title&gt;Google&lt;/title&gt;   &lt;script&gt;(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&amp;&amp;(!a.getAttribute||!(b=a.getAttribute(\"eid\")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&amp;&amp;(!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&amp;&amp;window.google.vel&amp;&amp;window.google.vel.lu&amp;&amp;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&amp;&amp;-1==b.search(\"&amp;ei=\")){var h=google.getEI(e),c=\"&amp;ei=\"+h;-1==b.search(\"&amp;lei=\")&amp;&amp;((e=google.getLEI(e))?c+=\"&amp;lei=\"+e:h!=google.kEI&amp;&amp;(c+=\"&amp;lei=\"+google.kEI))}a=d||\"/\"+(g||\"gen_204\")+\"?atyp=i&amp;ct=\"+a+\"&amp;cad=\"+b+c+f+\"&amp;zx=\"+google.time();/^http:/i.test(a)&amp;&amp;google.https()&amp;&amp;(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&amp;&amp;!!location.hash.match('[#&amp;]((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&amp;&amp;window.performance.now&amp;&amp;(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]&amp;&amp;(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)&amp;&amp;a[c])return;google.csiReport&amp;&amp;google.csiReport(void 0,void 0,d)}};google.rolh=function(a){google.uolh();google.timers.load&amp;&amp;(google.timers.load.olh=a,window.addEventListener?window.addEventListener(\"load\",a,!1):window.attachEvent&amp;&amp;window.attachEvent(\"onload\",a))};google.uolh=function(){if(google.timers.load){var a=google.timers.load;a.olh&amp;&amp;(window.addEventListener?window.removeEventListener(\"load\",a.olh,void 0):window.attachEvent&amp;&amp;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
  </code></pre>
  
  <p>The odd thing is that when I set Tutorial to false later on in my app, it does set it to false. But then when I rerun the program, Tutorial is set back to true. However, I don't reset it in my code anywhere. This sometimes happens with firstRun as well but sometimes it DOES get set to false... Any ideas? I'll double check.</p>