简体   繁体   中英

Android app is crashing. I've tried the obvious solutions not sure where to go from here

The classic "null object reference" i assume it's because i've already created the sharedPreferences but i'm not sure of the solution, i anyone could help that would be great. The highscore class (below) "sharedPreferences prefs = ..." is where the error is and i'm now getting the error in the onCreate method of the MainActivity - displayHighscore() which calls the highscoreSetorGet method of the highscore class. where as before i was only getting this error the second time i called the function.

Highscore class

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

}

code for calling highscoreSetorGet at the point where it crashes

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

The Error

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) 

Try to change highscore.highscoreSetorGet(this, 0, 0, 0, 0, false); to highscore.highscoreSetorGet(MainActivity.this, 0, 0, 0, 0, false); .

If it won't help, try to use: SharedPreferences pref = context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);

In addition, when using constants from a class, you should call it from the Class it self.instead of hs.ctx.MODE_PRIVATE use Context.MODE_PRIVATE .

EDIT: Few comments, You shouldn't use static MainActivity ma = new MainActivity(); , use this instead. Please read the android code style guide. https://source.android.com/source/code-style.html . eg mContext instead of context and many more. If you are trying to create a Singelton class with highscore, that's not the way, read a guide for that.

As for the crash, Change in highscore class:

static void highscoreSetorGet(Context context, int min, int sec, int centi, int counter, boolean write) {
SharedPreferences pref = context.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);

and use in MainActivity:

highscore.highscoreSetorGet(context, 0, 0, 0, 0, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM