From ee647b76b44dc6448718f5f7fcf799e314352f7f Mon Sep 17 00:00:00 2001
From: rizkihalasan <13515095@std.stei.itb.ac.id>
Date: Wed, 21 Feb 2018 19:06:30 +0700
Subject: [PATCH] fix bug shared preferences

---
 .../com/example/leo/fitnessdiy/LoginActivity.java   | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/app/src/main/java/com/example/leo/fitnessdiy/LoginActivity.java b/app/src/main/java/com/example/leo/fitnessdiy/LoginActivity.java
index 2c93e5e..e123133 100644
--- a/app/src/main/java/com/example/leo/fitnessdiy/LoginActivity.java
+++ b/app/src/main/java/com/example/leo/fitnessdiy/LoginActivity.java
@@ -17,9 +17,16 @@ public class LoginActivity extends AppCompatActivity {
         String sharedPrefFile = "com.example.leo.fitnessdiy";
         final String BACKGROUND_KEY = "background";
         mPreferences = getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
-
-        int background = mPreferences.getInt(BACKGROUND_KEY, R.drawable.green_theme);
-        getWindow().getDecorView().setBackground(getResources().getDrawable(background));
+        int background = R.drawable.green_theme;
+        try{
+            background = mPreferences.getInt(BACKGROUND_KEY, R.drawable.green_theme);
+        } catch(ClassCastException e){
+            SharedPreferences.Editor preferencesEditor = mPreferences.edit();
+            preferencesEditor.putInt(BACKGROUND_KEY, background);
+            preferencesEditor.apply();
+        } finally {
+            getWindow().getDecorView().setBackground(getResources().getDrawable(background));
+        }
     }
 
     public void doLogin(View view) {
-- 
GitLab