我有一个游戏,main
活动使用startActivityForResult
调用其他三个活动 - 第一个(SignInActivity
)返回用户名,或允许创建新用户名;第二个(LevelChooser
)使用getSharedPreferences
查找具有该用户名称的首选项文件,或者创建一个新文件,显示用户目前的进度(未解锁的级别,获得的星标),并允许用户选择打任何解锁级别;第三个(GameActivity
)更新用户首选项文件,如果该级别成功完成,然后返回(main
)到LevelChooser
。在LevelChooser
我已覆盖onBackPressed
以返回SignInActivity;在GameActivity
中,onFinish
被覆盖,因此无论发生的情况如何,都会返回LevelChooser
。
现在,十分之九,这一切都完全按照预期工作,但有时并非如此:有时,LevelChooser
显示一组不正确的理论,而不是看到用户的实际星和级别。不可能的值(例如,一个级别显示为已锁定,但已完成三个星级)。这通常(但并非总是如此)如果您选择一个级别,然后在您第一次打开游戏时退出它:它将允许您播放显示为未锁定的任何级别,但GameActivity
无法保存您的如果你完成了这个级别,那么当你回到LevelChooser
时会显示相同的错误级别;或者,如果您退出LevelChooser
并重新选择相同的用户名,它将返回到预期的行为。我还设法通过反复启动级别并退出它们来重现错误 - 如果你尝试了足够的时间它最终会出错。对于我自己的用户名(并且,我想,对于所有用户),错误信息总是相同的,即问题是间歇性的,但是当它发生时,不是随机的。
我已经尝试过调试,但由于某种原因,(a)问题只发生在我的手机上,而不是在模拟器上,(b)在我的手机上进行调试(而不是跑步)时,它可以正常工作,否则,如果出错,只需终止(AFAIR甚至没有“X已经停止”对话框),而不是显示错误的关卡。我在调试中唯一能看到的是onCreate
活动的LevelChooser
有时会执行多次。
因为问题是间歇性的并且不能直接重现,我想知道我是否在不知不觉中假设某些异步过程已经/将会以及时,线性的方式完成,并且通常(但并非总是)有义务;或者,我认为我无法理解与Activity生命周期相关且重要的内容。否则,我很难过并猜测。
main Activity
:
public class MainActivity extends AppCompatActivity {
public ImageView splash;
private int GET_USER_NAME_CODE = 0;
private int GET_LEVEL_CODE = 1;
private int PLAY_GAME_CODE = 2;
private String user;
private int level;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
splash = (ImageView) findViewById(R.id.splashView);
splash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sign_intent = new Intent(MainActivity.this, SignInActivity.class);
startActivityForResult(sign_intent, GET_USER_NAME_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed
if(requestCode==GET_USER_NAME_CODE)
{
user=data.getStringExtra("USER");
Intent intent = new Intent(MainActivity.this, LevelChooser.class);
intent.putExtra("user", user);
startActivityForResult(intent, GET_LEVEL_CODE);
}
else {
if(requestCode==GET_LEVEL_CODE) {
level=data.getIntExtra("LEVEL", 0);
if(level==-1) {
Intent sign_intent = new Intent(MainActivity.this, SignInActivity.class);
startActivityForResult(sign_intent, GET_USER_NAME_CODE);
}
else {
Intent intent = new Intent(MainActivity.this, GameActivity.class);
intent.putExtra("user", user);
intent.putExtra("level", level);
startActivityForResult(intent, PLAY_GAME_CODE);
}
}
else {
if(requestCode==PLAY_GAME_CODE) {
user=data.getStringExtra("user");
Intent intent = new Intent(MainActivity.this, LevelChooser.class);
intent.putExtra("user", user);
startActivityForResult(intent, GET_LEVEL_CODE);
}
}
}
}
LevelChooser
:
public class LevelChooser extends AppCompatActivity {
private String user;
private ImageView[] level;
private Boolean[] locked;
private int[] stars;
private SharedPreferences userprefs;
private SharedPreferences.Editor prefseditor;
private Boolean createNewPrefsFile = false;
private int tempResIdVisible;
private int tempResIdInvisible;
private ImageView tempView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.levelchooser);
}
@Override
protected void onStart() {
super.onStart();
level = new ImageView[21];
locked = new Boolean[21];
stars = new int[21];
user = getIntent().getStringExtra("user");
userprefs = getSharedPreferences(user, MODE_PRIVATE);
prefseditor = userprefs.edit();
//level numbers for views etc start from 1 to match images etc
level[0] = null;
locked[0] = null;
stars[0] = 0;
locked[1] = false;
level[1] = (ImageView) findViewById(R.id.level1);
level[1].setOnClickListener(new LevelClickListener(level[1], 1));
if (!userprefs.contains("stars1")) {
createNewPrefsFile = true;
}
stars[1] = userprefs.getInt("stars1", 0);
if (stars[1] != 0) {
for (int j = 1; j < 4; j++) {
tempResIdInvisible = getResources().getIdentifier("stars" + j + "_1", "id", getPackageName());
tempView = (ImageView) findViewById(tempResIdInvisible);
tempView.setVisibility(View.INVISIBLE);
}
tempResIdVisible = getResources().getIdentifier("stars" + stars[1] + "_1", "id", getPackageName());
tempView = (ImageView) findViewById(tempResIdVisible);
tempView.setVisibility(View.VISIBLE);
}
for (int i = 2; i < 21; i++) {
locked[i] = userprefs.getBoolean("locked" + i, true);
if (locked[i]) {
tempResIdVisible = getResources().getIdentifier("padlock" + i, "id", getPackageName());
tempResIdInvisible = getResources().getIdentifier("level" + i, "id", getPackageName());
} else {
tempResIdVisible = getResources().getIdentifier("level" + i, "id", getPackageName());
tempResIdInvisible = getResources().getIdentifier("padlock" + i, "id", getPackageName());
level[i] = (ImageView) findViewById(tempResIdVisible);
level[i].setOnClickListener(new LevelClickListener(level[i], i));
}
tempView = (ImageView) findViewById(tempResIdVisible);
tempView.setVisibility(View.VISIBLE);
tempView = (ImageView) findViewById(tempResIdInvisible);
tempView.setVisibility(View.INVISIBLE);
stars[i] = userprefs.getInt("stars" + i, 0);
if (stars[i] != 0) {
for (int j = 1; j < 4; j++) {
tempResIdInvisible = getResources().getIdentifier("stars" + j + "_" + i, "id", getPackageName());
tempView = (ImageView) findViewById(tempResIdInvisible);
tempView.setVisibility(View.INVISIBLE);
}
tempResIdVisible = getResources().getIdentifier("stars" + stars[i] + "_" + i, "id", getPackageName());
tempView = (ImageView) findViewById(tempResIdVisible);
tempView.setVisibility(View.VISIBLE);
}
}
if (createNewPrefsFile) {
for (int i = 1; i < 21; i++) {
prefseditor.putBoolean("locked" + i, locked[i]);
prefseditor.putInt("stars" + i, stars[i]);
prefseditor.commit();
}
}
}
GameActivity
:
public class GameActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
//TTS Object
private TextToSpeech myTTS;
//TTS status check code
private int MY_DATA_CHECK_CODE = 0;
private int level;
private String user;
private PhonemeGroup levelGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
level = getIntent().getIntExtra("level", 0);
user = getIntent().getStringExtra("user");
levelGroup = initializeLevels(level);
Intent checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
}
@Override
public void finish() {
Intent intent = new Intent();
intent.putExtra("user", user);
setResult(2, intent);
super.finish();
}
@Override
public void onStop() {
if (myTTS != null) {
myTTS.stop();
}
super.onStop();
}
@Override
public void onDestroy() {
if (myTTS != null) {
myTTS.shutdown();
}
Button ok_button = (Button) findViewById(R.id.button);
ok_button.setOnClickListener(null);
ImageView tickImageView = (ImageView) findViewById(R.id.tickImageView);
tickImageView.setOnClickListener(null);
ImageView starsView = (ImageView) findViewById(R.id.starsImageView);
starsView.setOnClickListener(null);
super.onDestroy();
unbindDrawables(findViewById(R.id.GameParentView));
System.gc();
}
答案 0 :(得分:0)
事实证明问题不在于getSharedPreferences()
,而是由user
返回的变量getIntent().getStringExtra()
有时 - 在我发现难以确定的情况下或者可靠地复制 - null
,如more info。该字符串随后用作getSharedPreferences()
的参数,从而为用户null
生成一组已保存的结果。每次问题再次出现时,都会显示这些结果(在问题的第二个图像中显示)。
解决方案 - 虽然它仍然无法解释为什么getStringExtra()
应该返回null
- 但是通过将以下代码放入两者的onCreate
来保护额外内容需要有效用户的活动:
if(getIntent().getExtras()==null || getIntent().getStringExtra("user")==null) {
Intent intent = new Intent();
setResult(0, intent);
finish();
}
如果在加载新活动的布局之前执行此操作,则会返回主活动并再次尝试(可以这么说)而不会让用户看到任何问题。