Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在活动之间发送字符串_Java_Android - Fatal编程技术网

Java 在活动之间发送字符串

Java 在活动之间发送字符串,java,android,Java,Android,我想向显示游戏最高分数的HighScore活动发送一个值 我正在将游戏活动的分数保存在共享首选项中,并尝试将分数(字符串值)发送到活动HighScore Intent i = new Intent(this, HighScoreActivity.class); i.putExtra("classicHighScore", highScore); 看起来你是有意发送字符串的。 也就是说,在你的高分活动中,写 String highScore = getIntent.getStringExtra

我想向显示游戏最高分数的
HighScore
活动发送一个值

我正在将游戏活动的分数保存在共享首选项中,并尝试将分数(字符串值)发送到活动
HighScore

Intent i = new Intent(this, HighScoreActivity.class); 
i.putExtra("classicHighScore", highScore);

看起来你是有意发送字符串的。 也就是说,在你的高分活动中,写

String highScore = getIntent.getStringExtra("classicHighScore", "");
好了, 字符串highScore现在具有您的值

您可以将其记录以检查:

Log.d("HIGH SCORE VALUE ", highScore);

您需要在第一个活动中将数据保存在SharedReferences对象中,并在另一个活动中读取数据

在第一个活动中使用SharedReferences保存分数:

SharedPreferences preferences = getSharedPreferences("SharedScore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("classicHighScore", highScore);
editor.commit();
阅读其他活动中SharedReference的分数:

SharedPreferences preferences = getSharedPreferences("SharedScore", MODE_PRIVATE);
int score = preferences.getInt("classicHighScore", -1);

如果要在活动之间发送字符串,则必须创建绑定

Intent i = new Intent(this, HighScoreActivity.class); 

Bundle bundle = new Bundle();
bundle.putString("classicHighScore", highScore);
i.putExtra(bundle);
读取HighScoreActivity中的de参数

Bundle arguments = getIntent().getExtras();
String classicHighScore = arguments.getString("classicHighScore");

您尚未提供要解决的问题。请提出一个特定的问题。当您将其保存在共享首选项中时,为什么需要将其发送到其他活动?您可以在任何活动中获取值,而无需使用共享首选项将其传递给活动。