当我点击新的游戏按钮时,它不会刷新我的游戏它仍然在旧的任何人可以帮助我如何解决它。这是我的代码
菜单游戏类
public class MenuScreen extends ListActivity {
public static final String NEW_GAME = "new_game";
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String[] ACTIVITY_CHOICES = new String[] {
getString(R.string.menu_new_game),
getString(R.string.menu_prefs)
};
setContentView(R.layout.activity_menu_screen);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ACTIVITY_CHOICES));
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setTextFilterEnabled(true);
OnItemClickListener myOnItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
switch(arg2) {
case 0:
startGame();
break;
case 1:
startPrefs();
break;
default: break;
}
}
};
getListView().setOnItemClickListener(myOnItemClickListener);
}
private void startGame() {
Intent launchGame = new Intent(this, MainActivity.class);
launchGame.putExtra("SENDER", "MenuScreen");
startActivity(launchGame);
}
private void startPrefs() {
Intent launchPrefs = new Intent(this, Setting.class);
startActivity(launchPrefs);
}}
玩游戏类
public class MainActivity extends Activity {
public static final String COME_FROM = "come_from";
private int[] id_mc = new int[16];
private Integer[][] img_mc = new Integer[16][2];
private Button[] myMcs = new Button[16];
private int mc_counter = 0;
private int firstid = 0;
private int secondid = 0;
private Boolean mc_isfirst = false;
private int correctcounter = 0;
private TextView tFeedback;
private MediaPlayer mp;
private Boolean b_snd_inc, b_snd_cor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
initGame();
}
private void initGame() {
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
b_snd_cor =settings.getBoolean("play_sound_when_correct", true);
b_snd_inc =settings.getBoolean("play_sound_when_incorrect", true);
mc_counter = 0;
firstid = 0;
secondid = 0;
mc_isfirst = false;
correctcounter = 0;
tFeedback = (TextView) findViewById(R.id.mc_feedback);
// setup button listeners
Button startButton = (Button) findViewById(R.id.game_menu);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startMenu();
}
});
Button settingsButton = (Button) findViewById(R.id.game_settings);
settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startPrefs();
}
});
// fill arrays with resources
id_mc[0] = R.id.mc0;
id_mc[1] = R.id.mc1;
id_mc[2] = R.id.mc2;
id_mc[3] = R.id.mc3;
id_mc[4] = R.id.mc4;
id_mc[5] = R.id.mc5;
id_mc[6] = R.id.mc6;
id_mc[7] = R.id.mc7;
id_mc[8] = R.id.mc8;
id_mc[9] = R.id.mc9;
id_mc[10] = R.id.mc10;
id_mc[11] = R.id.mc11;
id_mc[12] = R.id.mc12;
id_mc[13] = R.id.mc13;
id_mc[14] = R.id.mc14;
id_mc[15] = R.id.mc15;
img_mc[0][0] = R.drawable.back1;
img_mc[0][1] = R.drawable.ic_img1;
img_mc[1][0] = R.drawable.back2;
img_mc[1][1] = R.drawable.ic_img2;
img_mc[2][0] = R.drawable.back3;
img_mc[2][1] = R.drawable.ic_img3;
img_mc[3][0] = R.drawable.back4;
img_mc[3][1] = R.drawable.ic_img4;
img_mc[4][0] = R.drawable.back5;
img_mc[4][1] = R.drawable.ic_img5;
img_mc[5][0] = R.drawable.back6;
img_mc[5][1] = R.drawable.ic_img6;
img_mc[6][0] = R.drawable.back7;
img_mc[6][1] = R.drawable.ic_img7;
img_mc[7][0] = R.drawable.back8;
img_mc[7][1] = R.drawable.ic_img8;
img_mc[8][0] = R.drawable.back1;
img_mc[8][1] = R.drawable.ic_img1;
img_mc[9][0] = R.drawable.back2;
img_mc[9][1] = R.drawable.ic_img2;
img_mc[10][0] = R.drawable.back3;
img_mc[10][1] = R.drawable.ic_img3;
img_mc[11][0] = R.drawable.back4;
img_mc[11][1] = R.drawable.ic_img4;
img_mc[12][0] = R.drawable.back5;
img_mc[12][1] = R.drawable.ic_img5;
img_mc[13][0] = R.drawable.back6;
img_mc[13][1] = R.drawable.ic_img6;
img_mc[14][0] = R.drawable.back7;
img_mc[14][1] = R.drawable.ic_img7;
img_mc[15][0] = R.drawable.back8;
img_mc[15][1] = R.drawable.ic_img8;
//Collections.shuffle(Arrays.asList(img_mc));
for (int i = 0; i < 16; i++) {
myMcs[i] = (Button) findViewById(id_mc[i]);
myMcs[i].setBackgroundResource(img_mc[i][0]);
myMcs[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int i = 0;
for (int n = 0; n < 16; n++) {
if (id_mc[n] == view.getId())
i = n;
}
doClickAction(view, i);
}
});
}
}
private void doClickAction(View v, int i)
{
v.setBackgroundResource(img_mc[i][1]);
mc_isfirst = !mc_isfirst;
// disable all buttons
for (Button b : myMcs) {
b.setEnabled(false);
}
if (mc_isfirst) {
// turning the first card
firstid = i;
// re enable all except this one
for (Button b : myMcs) {
if (b.getId() != firstid) {
b.setEnabled(true);
}
}
} else {
// turning the second card
secondid = i;
doPlayMove();
}
}
private void doPlayMove() {
mc_counter++;
if (img_mc[firstid][1] - img_mc[secondid][1] == 0) {
//correct
if (b_snd_cor) playSound(R.raw.correct);
waiting(200);
myMcs[firstid].setVisibility(View.INVISIBLE);
myMcs[secondid].setVisibility(View.INVISIBLE);
correctcounter++;
} else {
//incorrect
if (b_snd_inc) playSound(R.raw.incorrect);
waiting(400);
}
// reenable and turn cards back
for (Button b : myMcs) {
if (b.getVisibility() != View.INVISIBLE) {
b.setEnabled(true);
b.setBackgroundResource(R.drawable.memory_back);
for (int i = 0; i < 16; i++) {
myMcs[i].setBackgroundResource(img_mc[i][0]);
}
}
}
tFeedback.setText("" + correctcounter + " / " + mc_counter);
if (correctcounter > 7) {
Intent iSc = new Intent(getApplicationContext(), Scoreboard.class);
iSc.putExtra("com.gertrietveld.memorygame.SCORE", mc_counter);
startActivity(iSc);
}
}
public void playSound(int sound) {
mp = MediaPlayer.create(this, sound);
mp.setVolume((float).5,(float).5);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
public static void waiting(int n) {
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while ((t1 - t0) < (n));
}
private void startMenu() {
Intent launchMenu = new Intent(this, MenuScreen.class);
launchMenu.putExtra(COME_FROM,"PlayGame");
startActivity(launchMenu);
}
private void startPrefs() {
Intent launchPrefs = new Intent(this, Setting.class);
startActivity(launchPrefs);
}
////////////////////////////////
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "onRestart-sender is " , Toast.LENGTH_SHORT).show();
}
设置类
当用户设置他们可以选择禁用声音时,禁用后有两个按钮:保存回游戏并保存回菜单。但点击这两个按钮后它没有做任何事情。当我关闭应用程序并将其杀死然后再打开我在设置工作正常时设置的内容时,它可以正常工作。
public class Setting extends Activity {
private CheckBox ch_sound_correct, ch_sound_incorrect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
ch_sound_correct = (CheckBox) findViewById(R.id.ch_sound_correct);
ch_sound_incorrect = (CheckBox) findViewById(R.id.ch_sound_incorrect);
Button saveButtonMenu = (Button) findViewById(R.id.save_prefs);
saveButtonMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchMenu = new Intent(getBaseContext(), MenuScreen.class);
setPreferences(launchMenu);
}
});
Button saveButtonGame = (Button) findViewById(R.id.save_game);
saveButtonGame.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchGame = new Intent(getBaseContext(), MainActivity.class);
launchGame.putExtra("SENDER","Settings");
setPreferences(launchGame);
}
});
getPreferences();
}
private void getPreferences()
{
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
ch_sound_correct.setChecked(settings.getBoolean("play_sound_when_correct", true));
ch_sound_incorrect.setChecked(settings.getBoolean("play_sound_when_incorrect", true));
}
private void setPreferences(Intent theIntent)
{
SharedPreferences settings = getSharedPreferences("memoryPrefs", 0);
SharedPreferences.Editor prefeditor = settings.edit();
prefeditor.putBoolean("play_sound_when_correct", ch_sound_correct.isChecked());
prefeditor.putBoolean("play_sound_when_incorrect", ch_sound_incorrect.isChecked());
prefeditor.commit();
//Toast.makeText(this, "setPreferences",Toast.LENGTH_LONG);
startActivity(theIntent);
}
}
答案 0 :(得分:0)
您必须编写代码:
recreate();
活动 重新创建方法的更多帮助:
void android.app.Activity.recreate()
导致使用新实例重新创建此活动。这导致与何时基本相同的流程 由于配置更改而创建了Activity - 当前实例将经历其生命周期 onDestroy然后在它之后创建一个新实例。
完成强>