gpt4 book ai didi

android - SharedPreferences 字符串名称

转载 作者:行者123 更新时间:2023-11-30 01:54:49 24 4
gpt4 key购买 nike

我一直在开发一款应用程序,根据团队在特定标准中的表现对团队进行评分,我希望它能保存团队数据。因此,即使应用程序关闭,也不会丢失其数据。这是我的保存类

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;
}
}

不幸的是...每次我尝试运行我的代码时它都会崩溃这是我的异常(exception)

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)

谢谢大家帮我解决了我的问题!

最佳答案

您的上下文传递给 new Save() 的是应用程序上下文吗?尝试将其更改为 Activity 。

即如果您在 Activity 类中,请将 new Save(getApplicationContext()) 替换为 new Save(this),或者替换为 new如果在 Fragment 类中使用它,请保存(getActivity())

关于android - SharedPreferences 字符串名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com