无法将数据保存到SharedPreferences

时间:2017-05-04 11:40:44

标签: java android

当我忘记了存储卡SharePrefernce和错误时,我将数据保存到SharePrefernce有问题。

我的日志

 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.novum.smrtkarta.SharedPreference.saveCardToSharedPreference(android.content.Context, java.util.ArrayList)' on a null object reference
                                                                     at com.xxx.smrtkarta.activity.ScanQrCodeActivity$3$1.run(ScanQrCodeActivity.java:285)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

类将数据卡保存到SharePrefernce  ScanQrCodeActivity.java

Default app = (Default) getApplicationContext();
 cardList = app.getListCard();
 Card card = new Card(numberCard, path3, base32, OTP, nameCard, intervalTotp, getDate(), expirationDate);

  if (containsCard(cardList, numberCard, expirationDate)) {
               showAlterDialog("Błąd tworzenia karty", "Podana karta  istnieje w telefonie.");
       } else {
                       cardList.add(card);
                       app.setListCard(cardList);
                       cardList = new ArrayList<Card>();
                       cardList = app.getListCard();

                        //   Toast.makeText(getApplicationContext(),"Rozmair listy"+cardList.size(),Toast.LENGTH_SHORT).show();
                        sharedPreference.saveCardToSharedPreference(mContext,cardList); //Here comes the error message

SharedPreference.java

public class SharedPreference {

    public static final String PREFS_NAME = "LIST_CARD";
    public static final String CARD = "CARD";

    public SharedPreference() {
    }

    public void saveCardToSharedPreference(Context context, ArrayList<Card> cardList) {

        SharedPreferences settings;
        SharedPreferences.Editor editor;

        settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = settings.edit();
        Gson gson = new Gson();
        String jsonString = gson.toJson(cardList);
        editor.putString(CARD, jsonString);
        editor.commit();
    }
}

Default.java

public class Default extends Application {

    public ArrayList<Card> listCard = new ArrayList<Card>();

    public String uuidString;


    public String getUuidString() {
        return uuidString;
    }

    public void setUuidString(String uuidString) {
        this.uuidString = uuidString;
    }

    public ArrayList<Card> getListCard() {
        return listCard;
    }

    public void setListCard(ArrayList<Card> listCard) {
        this.listCard = listCard;
    }

    @Override

    public void onCreate() {
        super.onCreate();

        uk.co.chrisjenx.calligraphy.CalligraphyConfig.initDefault(new uk.co.chrisjenx.calligraphy.CalligraphyConfig.Builder()
                .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                .setFontAttrId(R.attr.fontPath)
                .build()
        );

    }
}

1 个答案:

答案 0 :(得分:0)

cardList.add(card);
app.setListCard(cardList);

cardList = new ArrayList();

cardList = app.getListCard();

在您的代码中注明大胆代码,然后尝试。