为什么我会出现这个错误;无法解析方法';GetSharedReferences(Java.lang.String.int)";
您好,在过去的几个月里,我一直在尝试从android Studio制作一款游戏,但目前我一直在尝试保存最高分,我遇到了一个错误“无法解析方法‘getSharedReferences(Java.lang.String.int)’”。我不知道该怎么办,我尝试过其他方法,但似乎不起作用,所以我尝试了这种方法。请帮忙。这是代码为什么我会出现这个错误;无法解析方法';GetSharedReferences(Java.lang.String.int)";,java,android,Java,Android,您好,在过去的几个月里,我一直在尝试从android Studio制作一款游戏,但目前我一直在尝试保存最高分,我遇到了一个错误“无法解析方法‘getSharedReferences(Java.lang.String.int)’”。我不知道该怎么办,我尝试过其他方法,但似乎不起作用,所以我尝试了这种方法。请帮忙。这是代码 public class GamePanel扩展了SurfaceView实现了SurfaceHolder.Callback { 公共静态最终整数宽度=856; 公共静态最终内部
public class GamePanel扩展了SurfaceView实现了SurfaceHolder.Callback
{
公共静态最终整数宽度=856;
公共静态最终内部高度=480;
公共静态最终移动速度=-5;
私人长时间吸烟;
私人长期任务;
私有主线程;
私人背景背景背景;
私人玩家;
私烟;
私人ArrayList导弹;
私有ArrayList topborder;
私人ArrayList botborder;
private Random rand=new Random();
私有整数最大边界高度;
私人身高;
私有布尔自上而下=true;
私有布尔botDown=true;
创建私有布尔值;
公共静态整数高分=0;
公共静态SharedReferences优先级;
私有字符串saveScore=“HighScore”;
//增加以减缓难度进展,减少以加快难度进展
私有int progressDenom=20;
私人爆炸;
私人长星台;
私有布尔重置;
私有布尔不相似;
私有布尔启动;
公共静态int高分;
公共游戏面板(上下文)
{
超级(上下文);
上下文优先;
SharedReferences prefs=this.getSharedReferences(“myPrefsKeys”,Context.MODE\u PRIVATE);
int oldScore=prefs.getInt(“高分”,0);
int newScore=Player.getScore()*3;
//仅当新分数较高时更新分数
如果(新闻核心>旧分数){
Editor edit=prefs.edit();
编辑.putInt(“高分”,新闻核心);
edit.commit();
高分=新闻核心;
}
String spackage=“com.knight.myfirstgame”;
HighScore=prefs.getInt(saveScore,0);
//将回调添加到surfaceholder以拦截事件
getHolder().addCallback(此);
//使gamePanel可聚焦,以便处理事件
设置聚焦(真);
}
@凌驾
public void surfacechange(SurfaceHolder持有者,int格式,int宽度,int高度){}
@凌驾
公共空间表面覆盖(表面覆盖物持有人){
布尔重试=真;
int计数器=0;
而(重试和计数器高度/4)maxBorderHeight=高度/4;
minBorderHeight=5+player.getScore()/progressDenom;
//检查底部边界碰撞
for(int i=0;igetSharedReferences
是上下文
的方法,以及上下文
的子类,如活动
GamePanel
扩展了视图
,但没有上下文
,因此没有这样的方法
你可以这么做
context.getSharedPreferences(...)
如果需要在构造函数之外执行此操作,而构造函数中没有变量上下文
,则可以执行此操作
this.getContext().getSharedPreferences(...)
您可以创建一个全局变量作为
private Context mContext;
因此,当您在GamePanel上时,可以添加以下行:
this.mContext = context;
因此,在调用SharedReferences
时,您可以按如下操作:
SharedPreferences prefs = mContext.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE);
编辑
我看不到您在哪里使用Context-pref
,因此我想您忘记将其声明为:Context-pref=Context;
您正在创建一个SharedReferences
对象作为public static SharedReferences prefs;
。为什么要在GamePanel()
中放置此行SharedReferences prefs
?您可以使用prefs
编辑2
如果我对代码所做的更改解决了您的问题,请尝试
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
public static final int WIDTH = 856;
public static final int HEIGHT = 480;
public static final int MOVESPEED = -5;
private long smokeStartTime;
private long missileStartTime;
private MainThread thread;
private Background bg;
private Player player;
private ArrayList<Smokepuff> smoke;
private ArrayList<Missile> missiles;
private ArrayList<TopBorder> topborder;
private ArrayList<BotBorder> botborder;
private Random rand = new Random();
private int maxBorderHeight;
private int minBorderHeight;
private boolean topDown = true;
private boolean botDown = true;
private boolean newGameCreated;
public static int HighScore = 0;
public static SharedPreferences prefs;
private String saveScore = "HighScore";
//increase to slow down difficulty progression, decrease to speed up difficulty progression
private int progressDenom = 20;
private Explosion explosion;
private long startReset;
private boolean reset;
private boolean dissapear;
private boolean started;
public static int highScore;
private Context mContext; //Add this line <------
public GamePanel(Context context)
{
super(context);
this.mContext = context; //Add this line <------
//Context pref;
//SharedPreferences prefs = this.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE);
//Your SharedPreferences are allready defined
prefs = mContext.getSharedPreferences("myPrefsKeys", Context.MODE_PRIVATE); //Add this line <---
int oldScore = prefs.getInt("highScore", 0);
int newScore = Player.getScore()*3;
//update score only if new score is higher
if(newScore > oldScore ){
Editor edit = prefs.edit();
edit.putInt("highScore", newScore);
edit.commit();
highScore = newScore;
}
String spackage = "com.knight.myfirstgame";
HighScore = prefs.getInt(saveScore, 0);
//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);
//make gamePanel focusable so it can handle events
setFocusable(true);
}
public class GamePanel扩展了SurfaceView实现了SurfaceHolder.Callback
{
公共静态最终整数宽度=856;
公共静态最终内部高度=480;
公共静态最终移动速度=-5;
私人长时间吸烟;
私人长期任务;
私有主线程;
私人背景背景背景;
私人玩家;
私烟;
私人ArrayList导弹;
私有ArrayList topborder;
私人ArrayList botborder;
private Random rand=new Random();
私有整数最大边界高度;
私人身高;
私有布尔自上而下=true;
私有布尔botDown=true;
创建私有布尔值;
公共静态整数高分=0;
公共静态SharedReferences优先级;
私有字符串saveScore=“HighScore”;
//增加以减缓难度进展,减少以加快难度进展
私有int progressDenom=20;
私人爆炸;
私人长星台;
私有布尔重置;
私有布尔不相似;
私有布尔启动;
公共静态int高分;
private Context mContext;//添加此行我需要通过活动访问pref。这是我的解决方案:
import android.app.Activity;
import android.content.SharedPreferences;
import static android.content.Context.MODE_PRIVATE;
public class Preferences {
public SharedPreferences preferences;
public void setPreferences(Activity activity, String key, String value){
preferences = activity.getApplicationContext().getSharedPreferences("preferencestorage", MODE_PRIVATE);
SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putString(key,value);
preferencesEditor.apply();
}
public String getPreferenceValue(Activity activity, String key){
SharedPreferences preferences = activity.getApplicationContext().getSharedPreferences("preferencestorage", MODE_PRIVATE);
return preferences.getString(key,"");
}
}
JavaScript与Java无关-我删除了标记。Java与Java有很大关系。如果构造函数正在传递上下文,为什么要他使用此代码……@Skizo你是对的,OP可以只做Context.getSharedReferences(…)
但是在字段中存储上下文没有意义,因为您可以使用getContext
。“但是在字段中存储上下文没有意义”,这被称为干净代码。