使用SharedPrefernces进行收藏

时间:2019-05-20 20:50:08

标签: java android sharedpreferences

我正在尝试使用SharedPrefernces在我的应用程序中添加最喜欢的功能,以下代码在我的手机上可以正常工作,但是在Google控制台中,在ANR和崩溃下,我得到此报告:

Caused by: java.lang.NullPointerException: 
  at maa.app.Activities.Pop.alreadyExist (Pop.java:26)

java.lang.RuntimeException:    at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:3303)   at
android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:3411)   at android.app.ActivityThread.-wrap12
(Unknown Source)   at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1994)   at android.os.Handler.dispatchMessage
(Handler.java:108)   at android.os.Looper.loop (Looper.java:166)   at
android.app.ActivityThread.main (ActivityThread.java:7529)   at
java.lang.reflect.Method.invoke (Method.java)   at
com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:245)   at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:921) Caused by: java.lang.NullPointerException:    at
maa.app.Activities.Pop.alreadyExist (Pop.java:26)  
at maa.app.Activities.Pop.onCreate (Pop.java:416)  
at android.app.Activity.performCreate (Activity.java:7383)   at
android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1218)   at
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:3256)   at
android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:3411)   at android.app.ActivityThread.-wrap12
(Unknown Source)   at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:1994)   at android.os.Handler.dispatchMessage
(Handler.java:108)   at android.os.Looper.loop (Looper.java:166)   at
android.app.ActivityThread.main (ActivityThread.java:7529)   at
java.lang.reflect.Method.invoke (Method.java)   at
com.android.internal.os.Zygote$MethodAndArgsCaller.run
(Zygote.java:245)   at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:921)

JAVA

    public boolean alreadyExist(String url, TinyDB tinydb) {
            ArrayList<Post> myfavSaved;
            myfavSaved = tinydb.getListObject("favPro", Post.class);
            if (myfavSaved != null && myfavSaved.size() > 0)
                for (int i = 0; i < myfavSaved.size(); i++) {
                    if (myfavSaved.get(i).getImgUrl().equalsIgnoreCase(url)) {
                        return true;
                    }
                }
            return false;
        }


 public void WriteInShared(String name, String url, TinyDB tinydb) {
        tinydb = new TinyDB(getApplicationContext());
        ArrayList<Post> postObjects = tinydb.getListObject("favPro", Post.class);
        ArrayList<Post> myfav = new ArrayList<>();
        for (Object objs : postObjects) {
            myfav.add((Post) objs);
        }
        Post post = new Post();
        post.setImgUrl(url);
        post.setName(name);
        myfav.add(post);
        tinydb.putListObject("favPro", myfav);
        Toast.makeText(getApplicationContext(), "Added to Favorite", Toast.LENGTH_SHORT).show();
    }

    public void removeFromArrayList(String url, TinyDB tinydb) {
        ArrayList<Post> myfavSaved;
        myfavSaved = tinydb.getListObject("favPro", Post.class);
        for (Iterator<Post> iterator = myfavSaved.iterator(); iterator.hasNext(); ) {
            Post item = iterator.next();
            if (item.getImgUrl().equalsIgnoreCase(url)) {
                iterator.remove();
                saveFav(myfavSaved, tinydb);
            }
        }
        Toast.makeText(getApplicationContext(), "Removed from  Favorite", Toast.LENGTH_SHORT).show();
    }

    public void saveFav(ArrayList<Post> arrayList, TinyDB tinydb) {
        tinydb.putListObject("favPro", arrayList);
    }

getListObject()方法:

public ArrayList<Post> getListObject(String key, Class<Post> mClass) {
    Gson gson = new Gson();
    ArrayList<String> objStrings = getListString(key);
    ArrayList<Post> objects = new ArrayList<Post>();
    for (String jObjString : objStrings) {
        Post value = gson.fromJson(jObjString, mClass);
        objects.add(value);
    }
    return objects;
}

有人可以帮我解决这个问题吗?

0 个答案:

没有答案