SharedPreferences字符串名称

时间:2015-09-05 23:06:03

标签: android sharedpreferences

我一直致力于开发一个应用程序,根据他们在某些标准中的表现对团队进行评分,我希望它能够保存团队数据。因此,如果应用关闭,它不会丢失数据。 这是我的保存类

package com.example.elliott.alliancepicker;

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

import com.example.elliott.alliancepicker.MainActivity;
import com.example.elliott.alliancepicker.Question;
import com.example.elliott.alliancepicker.Team;

import java.util.ArrayList;


public class Save {
    String[] TeamBaseData = null;
    String[] TeamSplitData = new String[11];
    int[] TeamScoreData = new int[9];
    private static String prefsFileName = "prefs";
    private SharedPreferences prefs;
    String compileSave = "";
    public Save(Context context) {
    prefs = context.getSharedPreferences(prefsFileName,     Context.MODE_PRIVATE);
}
public void SaveTeamScore(){
    SharedPreferences.Editor editor = prefs.edit();
    editor.putString(prefsFileName, compileSave);
    editor.apply();
}
public Team regenTeam(){
    String toSplit = "";
    int questionScore = 0;
    TeamBaseData = (compileSave.split("@"));
    for (int i = 0; i < TeamBaseData.length; i++){
        toSplit = TeamBaseData[i];
        TeamSplitData = toSplit.split("%");
        for (int e = 2; e < TeamSplitData.length; e++) {
            int x = 0;
            toSplit = TeamSplitData[i];
            questionScore = Integer.parseInt(toSplit);
            TeamScoreData[x] = questionScore;
            x++;
        }
        if (TeamSplitData.length != 1) {
            Team newTeam = new Team(TeamSplitData[0], TeamSplitData[1], TeamScoreData);
            return newTeam;
        }
    }
    return null;
}
public SharedPreferences getPreferences() {
    return prefs;
}

public void setCompileSave(String compileSave) {
    this.compileSave = this.compileSave + compileSave;
}
}

不幸的是......每当我尝试运行我的代码时,它都会崩溃 这是我的例外

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.elliott.alliancepicker/com.example.elliott.alliancepicker.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2322)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        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:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:169)
        at com.example.elliott.alliancepicker.Save.<init>(Save.java:24)

谢谢大家帮我解决问题!

2 个答案:

答案 0 :(得分:1)

您的上下文是否已传递给new Save()应用程序上下文?尝试将其更改为“活动”。

即。如果您在new Save(getApplicationContext())班级,则将new Save(this)替换为Activity,如果您在new Save(getActivity())班级中使用Fragment,则将其替换为<template name="jsonImport"> <form id="importJson"> <input type="text" id="collection"> <input type="file" id="files" name="file" /> </form> </template>

答案 1 :(得分:0)

尝试在主线程上创建对象

Handler mainHandler = new         Handler(context.getMainLooper());

Runnable myRunnable = new      Runnable() {
@Override 
    public void run() {....} // This is your    code
};
mainHandler.post(myRunnable);