经典" null对象引用"我认为这是因为我已经创建了sharedPreferences,但我不确定解决方案,任何人都可以提供帮助,这将是非常好的。 高分类(下)" sharedPreferences prefs = ..."是错误的地方,我现在在MainActivity的onCreate方法中得到错误 - displayHighscore()调用highscore类的highscoreSetorGet方法。和之前一样,我第二次调用函数时只得到了这个错误。
高分等级
public class highscore {
static highscore hs = new highscore();
static int currentHighscore[] = new int[4];
static void highscoreSetorGet(int min, int sec, int centi, int counter, boolean write) {
SharedPreferences pref = ma.context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
if(write) {
editor.putInt("min", min);
editor.putInt("sec", sec);
editor.putInt("centi", centi);
editor.putInt("counter", counter);
editor.commit();
}
if(!write) {
currentHighscore[0] = pref.getInt("min", 0);
currentHighscore[1] = pref.getInt("sec", 0);
currentHighscore[2] = pref.getInt("centi", 0);
currentHighscore[3] = pref.getInt("counter", 0);
}
}
}
用于在崩溃时调用highscoreSetorGet的代码
public class MainActivity extends AppCompatActivity {
static MainActivity ma = new MainActivity();
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this.getApplicationContext();
Music.playMusic(this.getApplicationContext(), 1, 0, 3, 0);
highscore.highscoreSetorGet(0, 0, 0, 0, false);
displayHighscore();
}
public void displayHighscore() {
highscore.highscoreSetorGet(0, 0, 0, 0, false);
TextView tv = (TextView)findViewById(R.id.highscoreView);
tv.setText("Current Highscore: " + Integer.toString(highscore.currentHighscore[0]) + ":" + Integer.toString(highscore.currentHighscore[1]) + "." + Integer.toString(highscore.currentHighscore[2]));
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sam.myfirstapp, PID: 18807
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.myfirstapp/com.example.sam.myfirstapp.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:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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 com.example.sam.myfirstapp.highscore.highscoreSetorGet(highscore.java:20)
at com.example.sam.myfirstapp.MainActivity.displayHighscore(MainActivity.java:61)
at com.example.sam.myfirstapp.MainActivity.onCreate(MainActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
尝试将highscore.highscoreSetorGet(this, 0, 0, 0, 0, false);
更改为
highscore.highscoreSetorGet(MainActivity.this, 0, 0, 0, 0, false);
。
如果它没有帮助,请尝试使用:
SharedPreferences pref = context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
另外,当使用类中的常量时,你应该从它自己的类中调用它。而不是hs.ctx.MODE_PRIVATE
使用Context.MODE_PRIVATE
。
编辑:
几条评论,
您不应该使用static MainActivity ma = new MainActivity();
,而是使用this
。
请阅读android代码风格指南。 https://source.android.com/source/code-style.html。例如mContext而不是上下文等等。
如果您正在尝试创建一个高分的Singelton课程,那不是那样,请阅读相关指南。
至于崩溃, 高分类的变化:
static void highscoreSetorGet(Context context, int min, int sec, int centi, int counter, boolean write) {
SharedPreferences pref = context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
并在MainActivity中使用:
highscore.highscoreSetorGet(context, 0, 0, 0, 0, false);