如何在Android Studio中使用内部内存



我在安卓工作室上制作应用程序才大约3个月,我正在为Higher或Lower制作应用程序。重点是,每当用户按下按钮时,我都会弹出一个得分很高的吐司。我知道如何制作OnClick功能,但我想显示用户自安装应用程序以来的最高分数,而不仅仅是自打开应用程序以来。当我按下一个叫做"显示"的按钮时,我需要显示这些;高分";有人知道怎么做吗?如果你需要更多信息,只需询问即可。我是新来的堆栈溢出。

这是活动代码:

package com.nicorodriguez.yarokshigherorlower;
import androidx.appcompat.app.AppCompatActivity;
import java.util.*;
import java.util.concurrent.TimeUnit;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int num;
int guesses = 0;
int highscore = getSavedHighScore();
private static final String SAVED_HIGH_SCORE_INT = "high_score";
private void saveHighScore(int highscore) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor spe = sp.edit();
spe.putInt(SAVED_HIGH_SCORE_INT, highscore);
spe.apply();
}
private int getSavedHighScore(){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getInt(SAVED_HIGH_SCORE_INT, -1);
}
public void mouseClicked (View view) {
EditText editText = (EditText) findViewById(R.id.guess);
int guessnum = Integer.parseInt(editText.getText().toString());
if(guessnum > num){
Toast.makeText(this, "Guess Lower.", Toast.LENGTH_LONG).show();
guesses += 1;
}else if(guessnum < num) {
Toast.makeText(this, "Guess Higher.", Toast.LENGTH_LONG).show();
guesses += 1;
} else {
Random rand = new Random();
guesses += 1;
String guesstring = Integer.toString(guesses);
String highstring = Integer.toString(highscore);
Toast.makeText(this, "For your trouble, please try again.", Toast.LENGTH_LONG).show();
if(guesses < highscore){
saveHighScore(guesses);
Toast.makeText(this, "New High Score! nScore: " + guesstring, Toast.LENGTH_LONG).show();
if(highscore != 9999999) {
Toast.makeText(this, "nPrevious High Score: " + highstring, Toast.LENGTH_LONG).show();
}
highscore = guesses;
} else if(guesses == highscore) {
Toast.makeText(this, "Aww... What a shame. You tied the high score! nScore: " + guesstring, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Score: " + guesstring + "nHigh Score: " + highstring, Toast.LENGTH_LONG).show();
}
num = rand.nextInt(1000) + 1;
int guesses = 0;
}
Log.i("Info", Integer.toString(num));
}
public void highClick (View view) {
getSavedHighScore();
Toast.makeText(this, "Your all-time high score is " + highscore, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random rand = new Random();
num = rand.nextInt(1000) + 1;
getSavedHighScore();
}
}

这是Stacktrace过滤器:错误它真的很长。我希望这就是你想要的:

2020-07-17 11:38:34.040 344-344/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-07-17 11:38:34.045 344-344/? E/Zygote: accessInfo : 1
2020-07-17 11:38:34.383 344-344/com.nicorodriguez.yarokshigherorlower E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nicorodriguez.yarokshigherorlower, PID: 344
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nicorodriguez.yarokshigherorlower/com.nicorodriguez.yarokshigherorlower.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:3042)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
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:179)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:526)
at com.nicorodriguez.yarokshigherorlower.MainActivity.getSavedHighScore(MainActivity.java:32)
at com.nicorodriguez.yarokshigherorlower.MainActivity.<init>(MainActivity.java:20)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3030)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7156) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

将以下内容添加到您的活动中:

private static final String SAVED_HIGH_SCORE_INT = "high_score";
private void saveHighScore(int highScore) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor spe = sp.edit();
spe.putInt(SAVED_HIGH_SCORE_INT, highScore);
spe.apply();
}
private int getSavedHighScore() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getInt(SAVED_HIGH_SCORE_INT, -1);
}

每当游戏结束时调用saveHighScore(newHighScore)。在按钮点击监听器的onClick方法中调用getSavedHighScore,并在Toast中使用结果。

最新更新