2016-03-14 132 views
-1

我有以下我用来管理我的共享首选项的类。这工作完全正常,但现在我添加了两种方法getStoredPhotos()setStoredPhotos()以及变量STORED_PHOTOSstored_photos。现在我收到以下错误。该行logcat的指的是这是一个followed_cities = session.getFollowedCities(); 但是这个工作完全正常之前,我加入我提到的代码,我没有改变getFollowedCities()方法可言,因此我的困惑:预计BEGIN_ARRAY,但是BEGIN_OBJECT

FATAL EXCEPTION: main 
 
                   Process: citylife.com.city, PID: 5583 
 
                   com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 
 
                    at com.google.gson.Gson.fromJson(Gson.java:822) 
 
                    at com.google.gson.Gson.fromJson(Gson.java:775) 
 
                    at com.google.gson.Gson.fromJson(Gson.java:724) 
 
                    at citylife.com.city.SessionManager.getFollowedCities(SessionManager.java:68) 
 
                    at citylife.com.city.FragmentCTesting.onAttach(FragmentCTesting.java:114) 
 
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907) 
 
                    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846) 
 
                    at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860) 
 
                    at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130) 
 
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1103) 
 
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:951) 
 
                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473) 
 
                    at android.view.View.measure(View.java:17590) 
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
 
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
 
                    at android.view.View.measure(View.java:17590) 
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
 
                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436) 
 
                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722) 
 
                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613) 
 
                    at android.view.View.measure(View.java:17590) 
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653) 
 
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 
 
                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748) 
 
                    at android.view.View.measure(View.java:17590) 
 
                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030) 
 
                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174) 
 
                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395) 
 
                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062) 
 
                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5871) 
 
                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 
 
                    at android.view.Choreographer.doCallbacks(Choreographer.java:580) 
 
                    at android.view.Choreographer.doFrame(Choreographer.java:550) 
 
                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 
 
                    at android.os.Handler.handleCallback(Handler.java:739) 
 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
 
                    at android.os.Looper.loop(Looper.java:135) 
 
                    at android.app.ActivityThread.main(ActivityThread.java:5536) 
 
                    at java.lang.reflect.Method.invoke(Native Method) 
 
                    at java.lang.reflect.Method.invoke(Method.java:372) 
 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397) 
 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192) 
 
                    Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 
 
                    at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) 
 
                    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) 
 
                    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) 
 
                    at com.google.gson.Gson.fromJson(Gson.java:810) 
 
                    at com.google.gson.Gson.fromJson(Gson.java:775)  
 
                    at com.google.gson.Gson.fromJson(Gson.java:724)  
 
                    at citylife.com.city.SessionManager.getFollowedCities(SessionManager.java:68)  
 
                    at citylife.com.city.FragmentCTesting.onAttach(FragmentCTesting.java:114)  
 
                    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)  
 
                    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846)  
 
                    at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860)  
 
                    at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)  
 
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:1103)  
 
                    at android.support.v4.view.ViewPager.populate(ViewPager.java:951)  
 
                    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)  
 
                    at android.view.View.measure(View.java:17590)  
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)  
 
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)  
 
                    at android.view.View.measure(View.java:17590)  
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)  
 
                    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)  
 
                    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)  
 
                    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)  
 
                    at android.view.View.measure(View.java:17590)  
 
                    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5653)  
 
                    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)  
 
                    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2748)  
 
                    at android.view.View.measure(View.java:17590)  
 
                    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2030)  
 
                    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1174)  
 
                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1395)  
 
                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)  
 
                    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5871)  
 
                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)  
 
                    at android.view.Choreographer.doCallbacks(Choreographer.java:580)  
 
                    at android.view.Choreographer.doFrame(Choreographer.java:550)  
 
                    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)  
 
                    at android.os.Handler.handleCallback(Handler.java:739)  
 
                    at android.os.Handler.dispatchMessage(Handler.java:95)  
 
                    at android.os.Looper.loop(Looper.java:135)  
 
                    at android.app.ActivityThread.main(ActivityThread.java:5536)  
 
                    at java.lang.reflect.Method.invoke(Native Method)  
 
                    at java.lang.reflect.Method.invoke(Method.java:372)  
 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1397)  
 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192) 

public class SessionManager { 
private static String TAG = SessionManager.class.getSimpleName(); 
SharedPreferences pref; 

SharedPreferences.Editor editor; 
Context _context; 
int PRIVATE_MODE = 0; 
private static final String PREF_NAME = "Login"; 
private static final String KEY_IS_LOGGEDIN = "isLoggedIn"; 
public static final String USERNAME = "username"; 
public static final String MY_LIST = "my_list"; 
public static final String MY_LIST2 = "my_list"; 
private static final Type FOLLOWED_CITIES_TYPE = new TypeToken<ArrayList<CityShort>>() {}.getType(); 
public static final String EMAIL = "email"; 
private static final Type STORED_PHOTOS = new TypeToken<HashMap<String,ArrayList<ImageDisplayer>>>() {}.getType(); 
public static final String TESTSTRING = "test_string"; 
private ArrayList<CityShort> followed_cities = new ArrayList<>(); 
private HashMap<String, ArrayList<ImageDisplayer>> stored_photos = new HashMap<>(); 

public SessionManager(Context context) { 
    this._context = context; 
    pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
    editor = pref.edit(); 
} 

public void setLogin(boolean isLoggedIn, String username, String email) { 

    editor.putBoolean(KEY_IS_LOGGEDIN, isLoggedIn); 
    editor.putString(USERNAME, username); 
    editor.putString(EMAIL, email); 

    // commit changes 
    editor.commit(); 

    Log.d(TAG, "User login session modified!"); 
} 
public void logOut() { 
    editor.putBoolean(KEY_IS_LOGGEDIN,false); 
    editor.clear(); 
    editor.commit(); 
} 

public void setTestString(String test) { 
    editor.putString(TESTSTRING, test); 
    editor.commit(); 
} 
public ArrayList<CityShort> getFollowedCities() { 
    followed_cities = new Gson().fromJson(pref.getString(MY_LIST, null), FOLLOWED_CITIES_TYPE); 
    if (followed_cities == null) { 
     followed_cities = new Gson().fromJson(pref.getString(MY_LIST, null), FOLLOWED_CITIES_TYPE); 
     if(followed_cities == null){ 
      followed_cities = new ArrayList<CityShort>(); 
     } 
    } 
    return followed_cities; 
} 
public void setFollowedCities(ArrayList<CityShort> listwriter) { 
    this.followed_cities = listwriter; 
    editor.putString(MY_LIST, new Gson().toJson(listwriter)); 
    editor.commit(); 
} 
public HashMap<String,ArrayList<ImageDisplayer>> getStoredPhotos() { 
    stored_photos = new Gson().fromJson(pref.getString(MY_LIST2, null), STORED_PHOTOS); 
    if (stored_photos == null) { 
     stored_photos = new Gson().fromJson(pref.getString(MY_LIST2, null), STORED_PHOTOS); 
     if(stored_photos == null){ 
      stored_photos = new HashMap<>(); 
     } 
    } 
    return stored_photos; 
} 
public void setStoredPhotos(String cityId, ArrayList<ImageDisplayer> images) { 
    stored_photos.put(cityId,images); 
    editor.putString(MY_LIST2, new Gson().toJson(stored_photos)); 
    editor.commit(); 
} 
public HashMap<String, String> getTestString(){ 
    HashMap<String, String> user = new HashMap<String, String>(); 
    // user name 
    user.put(TESTSTRING, pref.getString(TESTSTRING, null)); 

    // return user 
    return user; 
} 
public HashMap<String, String> getUserDetails(){ 
    HashMap<String, String> user = new HashMap<String, String>(); 
    // user name 
    user.put(USERNAME, pref.getString(USERNAME, null)); 

    // return user 
    return user; 
} 
public boolean isLoggedIn(){ 
    return pref.getBoolean(KEY_IS_LOGGEDIN, false); 
}} 
+0

该异常是第1行的JsonSyntaxException。您是否更改了输入JSON? –

+0

所有我改变的是添加两个方法来存储HashMap。这意味着我也将HashMaps存储在我的共享首选项中,但是在此我正在检索其他内容,而不是HashMaps – Alk

+0

它看起来像您的JSON(您的设置文件)格式不正确。我猜测你可能正在以某种方式编写或阅读某些东西,而这种方式是在解析某些不期望的东西。 –

回答

0

的问题在这里:

public static final String MY_LIST = "my_list"; 
public static final String MY_LIST2 = "my_list"; 

不得不因为否则疫情周报改变其中之一的名称两个都是一样的。

相关问题