如何修复:“e/androidruntime:fatal exception:main”?

nkoocmlb  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(121)

我正在开发一个应用程序,每次我运行它时,当我在堆栈跟踪中查看时收到这个消息,我的应用程序就会崩溃。
e/androidruntime:致命异常:主进程:com.example.mylibary,pid:7292 java.lang.runtimeexception:无法启动activity componentinfo{com.example.mylibary/com.example.mylibary.allreadyreadbooksactivity}:com.google.gson.jsonsyntaxexception:java.lang.illegalstateexception:应为begin_数组,但在第1行第2列路径$at处为begin_对象android.app.activitythread.performlaunchactivity(活动线程。java:3270)在android.app.activitythread.handlelaunchactivity(activitythread。java:3409)在android.app.servertransaction.launchactivityitem.execute(launchactivityitem。java:83)在android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor。java:135)在android.app.servertransaction.transactionexecutor.execute(transactionexecutor。java:95)在android.app.activitythread$h.handlemessage上。java:2016)在android.os.handler.dispatchmessage(handler。java:107)在android.os.looper.loop(looper。java:214)在android.app.activitythread.main(activitythread。java:7356)在java.lang.reflect.method.invoke(本机方法)位于com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit)。java:492)在com.android.internal.os.zygoteinit.main(zygoteinit。java:930)原因:com.google.gson.jsonsyntaxexception:java.lang.illegalstateexception:应为begin\ U数组,但在第1行第2列路径$at处为begin\ U对象com.google.gson.gson.fromjson(gson。java:944)在com.google.gson.gson.fromjson(gson。java:897)在com.google.gson.gson.fromjson(gson。java:846)在com.example.mylibary.utils.getallreadyreadbooks(utils。java:94)在com.example.mylibary.allreadyreadbooksactivity.oncreate(allreadyreadbooksactivity。java:25)在android.app.activity.performcreate(活动。java:7802)在android.app.activity.performcreate(activity。java:7791)在android.app.instrumentation.callactivityoncreate(instrumentation。java:1299)在android.app.activitythread.performlaunchactivity(activitythread。java:3245)在android.app.activitythread.handlelaunchactivity(activitythread。java:3409)  在android.app.servertransaction.launchactivityitem.execute(launchactivityitem。java:83)  在android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor。java:135)  在android.app.servertransaction.transactionexecutor.execute(transactionexecutor。java:95)  在android.app.activitythread$h.handlemessage上。java:2016)  在android.os.handler.dispatchmessage(handler。java:107)  在android.os.looper.loop(looper。java:214)  在android.app.activitythread.main(activitythread。java:7356)  位于java.lang.reflect.method.invoke(本机方法)  在com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit。java:492)  在com.android.internal.os.zygoteinit.main(zygoteinit。java:930)  原因:java.lang.illegalstateexception:应为begin\u数组,但在com.google.gson.stream.jsonreader.beginarray(jsonreader)的第1行第2列路径$处为begin\u对象。java:351)在com.google.gson.internal.bind.collectiontypeadapterfactory$adapter.read(collectiontypeadapterfactory。java:80)在com.google.gson.internal.bind.collectiontypeadapterfactory$adapter.read(collectiontypeadapterfactory。java:61)在com.google.gson.gson.fromjson(gson。java:932)在com.google.gson.gson.fromjson(gson。java:897)  在com.google.gson.gson.fromjson(gson。java:846)  在com.example.mylibary.utils.getallreadyreadbooks(utils。java:94)  在com.example.mylibary.allreadyreadbooksactivity.oncreate(allreadyreadbooksactivity。java:25)  在android.app.activity.performcreate(activity。java:7802)  在android.app.activity.performcreate(activity。java:7791)  在android.app.instrumentation.callactivityoncreate(instrumentation。java:1299)  在android.app.activitythread.performlaunchactivity(activitythread。java:3245)  在android.app.activitythread.handlelaunchactivity(activitythread。java:3409)  在android.app.servertransaction.launchactivityitem.execute(launchactivityitem。java:83)  在android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor。java:135)  在android.app.servertransaction.transactionexecutor.execute(transactionexecutor。java:95)  在android.app.activitythread$h.handlemessage上。java:2016)  在android.os.handler.dispatchmessage(handler。java:107)  在android.os.looper.loop(looper。java:214)  在android.app.activitythread.main(activitythread。java:7356)  位于java.lang.reflect.method.invoke(本机方法)  在com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit。java:492)  在com.android.internal.os.zygoteinit.main(zygoteinit。java:930) 
当我看堆栈跟踪时,我什么也不懂。如果你有时间请回答,因为我真的需要它。
这是我的一些代码

package com.example.mylibary;

  import android.content.Context;
  import android.content.SharedPreferences;

  import com.google.gson.Gson;
  import com.google.gson.JsonArray;
  import com.google.gson.reflect.TypeToken;

  import java.lang.reflect.Type;
  import java.util.ArrayList;

public class Utils {

private static Utils instance;
SharedPreferences sharedPreferences;

private static final String ALL_BOOKS_KEY = "all_books";
private static final String ALL_READY_READ_BOOKS = "all_ready_read_books";
private static final String CURRENTLY_READING_BOOKS 
="currently_reading_books";
private static final String WANT_TO_READ_BOOKS = "want_to_read_books";
private static final String FAVORITE_BOOKS = "favorite_books";

private Utils(Context context) {

    sharedPreferences = context.getSharedPreferences("alterntet_db", Context.MODE_PRIVATE);

    if (null == getAllBooks()){
        initData();
    }
    SharedPreferences.Editor editor =sharedPreferences.edit();
    Gson gson = new Gson();

    if (null == getAllReadyReadBooks()){
        editor.putString(ALL_READY_READ_BOOKS, gson.toJson(new ArrayList<Books>()));
        editor.commit();
    }
    if (null == getCurrentlyReadingBooks()){
        editor.putString(CURRENTLY_READING_BOOKS, gson.toJson(new ArrayList<Books>()));
        editor.commit();
    }
    if (null == getWantToReadBooks()){
        editor.putString(WANT_TO_READ_BOOKS, gson.toJson(new ArrayList<Books>()));
        editor.commit();
    }
    if (null == getFavoritesBooks()){
        editor.putString(FAVORITE_BOOKS, gson.toJson(new ArrayList<Books>()));
        editor.commit();
    }
}

private void initData() {

    ArrayList<Books> books = new ArrayList<>();

    books.add(new Books(1, "harry potter","j.k roaling",500,"https://sites.google.com/site/veredk16/250px-Harry_Potter_-_GoF_Promo.jpg"
            ,"great book must read", "long desk"));
    books.add(new Books(2, "shantaram", "indian guy", 1050 , "https://www.e-vrit.co.il/Images/Products/YediotMasters/shantram_master.jpg"
            , "on india", "long Desk"));

    SharedPreferences.Editor editor = sharedPreferences.edit();
    Gson gson = new Gson();
    editor.putString(ALL_BOOKS_KEY, gson.toJson(books));
    editor.commit();
}

public static Utils getInstance(Context context) {
    if (null != instance){
        return instance;
    }else{
        instance = new Utils(context);
        return instance;
    }

}

public ArrayList<Books> getAllBooks() {
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Books>>(){}.getType();
    ArrayList<Books> books = gson.fromJson(sharedPreferences.getString(ALL_BOOKS_KEY, null), type);
    return books;
}

public  ArrayList<Books> getAllReadyReadBooks() {
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Books>>(){}.getType();
    ArrayList<Books> books = gson.fromJson(sharedPreferences.getString(ALL_READY_READ_BOOKS, null), type);
    return books;
}

public  ArrayList<Books> getCurrentlyReadingBooks() {
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Books>>(){}.getType();
    ArrayList<Books> books = gson.fromJson(sharedPreferences.getString(CURRENTLY_READING_BOOKS, null), type);
    return books;
}

public  ArrayList<Books> getWantToReadBooks() {
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Books>>(){}.getType();
    ArrayList<Books> books = gson.fromJson(sharedPreferences.getString(WANT_TO_READ_BOOKS, null), type);
    return books;
}

public  ArrayList<Books> getFavoritesBooks() {
    Gson gson = new Gson();
    Type type = new TypeToken<ArrayList<Books>>(){}.getType();
    ArrayList<Books> books = gson.fromJson(sharedPreferences.getString(FAVORITE_BOOKS, null), type);
    return books;
}

public Books getBookById(int id){
    ArrayList<Books> books = getAllBooks();
    if (null != books) {
        for (Books b : books) {
            if (b.getId() == id) {
                return b;
            }
        }
    }
    return null;
}

public boolean addAllReadyReadBook(Books book){
    ArrayList<Books> books = getAllReadyReadBooks();
    if (null!=books){
        if (books.add(book)){
            Gson gson = new Gson();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(ALL_READY_READ_BOOKS);
            editor.putString(ALL_READY_READ_BOOKS, gson.toJson(books));
            editor.commit();
            return true;
        }
    }
    return false;
}

public boolean addWantToReadBooks(Books book){
    ArrayList<Books> books = getWantToReadBooks();
    if (null!=books){
        if (books.add(book)){
            Gson gson = new Gson();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(WANT_TO_READ_BOOKS);
            editor.putString(WANT_TO_READ_BOOKS, gson.toJson(books));
            editor.commit();
            return true;
        }
    }
    return false;
}

public boolean addCurrentlyReadingBooks(Books book){
    ArrayList<Books> books = getCurrentlyReadingBooks();
    if (null!=books){
        if (books.add(book)){
            Gson gson = new Gson();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(CURRENTLY_READING_BOOKS);
            editor.putString(CURRENTLY_READING_BOOKS, gson.toJson(books));
            editor.commit();
            return true;
        }
    }
    return false;
}

public boolean addToFavoritesBooks(Books book){
    ArrayList<Books> books = getFavoritesBooks();
    if (null!=books){
        if (books.add(book)){
            Gson gson = new Gson();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(FAVORITE_BOOKS);
            editor.putString(FAVORITE_BOOKS, gson.toJson(books));
            editor.commit();
            return true;
        }
    }
    return false;
}

public boolean removeAllReadyReadBooks(Books book){
    ArrayList<Books> books = getAllReadyReadBooks();
    if (null!= book){
        for (Books b: books){
            if (b.getId() == book.getId()){
                if (books.remove(b)){
                    Gson gson = new Gson();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.remove(ALL_READY_READ_BOOKS);
                    editor.putString(ALL_READY_READ_BOOKS, gson.toJson(book));
                    editor.commit();
                    return true;
                }
            }
        }
    }
    return  false;
}

public boolean removeCurrentlyReadingBooks(Books book){
    ArrayList<Books> books = getCurrentlyReadingBooks();
    if (null!= book){
        for (Books b: books){
            if (b.getId() == book.getId()){
                if (books.remove(b)){
                    Gson gson = new Gson();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.remove(CURRENTLY_READING_BOOKS);
                    editor.putString(CURRENTLY_READING_BOOKS, gson.toJson(book));
                    editor.commit();
                    return true;
                }
            }
        }
    }
    return  false;
}

public boolean removeWantToReadBooks(Books book){
    ArrayList<Books> books = getWantToReadBooks();
    if (null!= book){
        for (Books b: books){
            if (b.getId() == book.getId()){
                if (books.remove(b)){
                    Gson gson = new Gson();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.remove(WANT_TO_READ_BOOKS);
                    editor.putString(WANT_TO_READ_BOOKS, gson.toJson(book));
                    editor.commit();
                    return true;
                }
            }
        }
    }
    return  false;
}

public boolean removeFavoritesBooks(Books book){
    ArrayList<Books> books = getFavoritesBooks();
    if (null!= book){
        for (Books b: books){
            if (b.getId() == book.getId()){
                if (books.remove(b)){
                    Gson gson = new Gson();
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.remove(FAVORITE_BOOKS);
                    editor.putString(FAVORITE_BOOKS, gson.toJson(book));
                    editor.commit();
                    return true;
                }
            }
        }
    }
    return  false;
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题