In my previous question it is because the value make the activity not opened so i changed to new code but it still force close
i was using bluestack as emulator so there is no Log Cat
this is my code to make random activity
public class menu extends Activity {
int time = 5, one, two, three, four, five, number;
RelativeLayout layout1, layout2;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
layout1 =(RelativeLayout)findViewById(R.id.layout1);
layout2 =(RelativeLayout)findViewById(R.id.layout2);
new CountDownTimer(5000, 1000) {
@Override
public void onFinish() {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
}
@Override
public void onTick(long millisUntilFinished) {
}
}.start();
button1 = (Button)findViewById(R.id.introbutton1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Level", 0);
editor.commit();
// Here, we are generating a random number
Random generator = new Random();
number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
// E.g., if the output is 1, the activity we will open is ActivityOne.class
case 1:
activity = activityone.class;
//to check the activity already opened or not, so he will do this logic
if(one == 1){
Random generatorone = new Random();
number = generatorone.nextInt(5) + 1;
}
break;
case 2:
activity = activitytwo.class;
if(two== 1){
Random generatortwo = new Random();
number = generatortwo.nextInt(5) + 1;
}
break;
case 3:
activity = activitythree.class;
if(three== 1){
Random generatorthree = new Random();
number = generatorthree.nextInt(5) + 1;
}
break;
case 4:
activity = activityfour.class;
if(four == 1){
Random generatorFour = new Random();
number = generatorFour.nextInt(5) + 1;
}
editor = pref.edit();
// Key,Value
editor.putInt("Activity", number);
editor.commit();
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and when each activity opened there is a code to make value of the activity == 1 (already opened)
buttonblack = (Button)findViewById(R.id.buttonblack);
buttonblack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
level++;
SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("Lifes", gamelifes);
editor.putInt("Level", level);
//sharedpreferences put int values to 1 (the activity already opened)
editor.putInt("One", 1);
editor.commit();
// Here, we are generating a random number
Random generator = new Random();
number = generator.nextInt(5) + 1;
// The '5' is the number of activities
Class activity = null;
// Here, we are checking to see what the output of the random was
switch(number) {
// E.g., if the output is 1, the activity we will open is ActivityOne.class
case 1:
activity = activityone.class;
if(one == 1){
Random generatorone = new Random();
number = generatorone.nextInt(5) + 1;
}
break;
case 2:
activity = activitytwo.class;
if(two== 1){
Random generatortwo = new Random();
number = generatortwo.nextInt(5) + 1;
}
break;
case 3:
activity = activitythree.class;
if(three== 1){
Random generatorthree = new Random();
number = generatorthree.nextInt(5) + 1;
}
break;
case 4:
activity = activityfour.class;
if(four == 1){
Random generatorFour = new Random();
number = generatorFour.nextInt(5) + 1;
}
break;
default:if(level == 5){
activity = activityfive.class;
}
editor = pref.edit();
// Key,Value
editor.putInt("Activity", number);
editor.commit();
}
// We use intents to start activities
Intent intent = new Intent(getBaseContext(), activity);
startActivity(intent);
}
});
}
There is something missing or wrong with my code that can't make the activity random properly?
can anyone help me to fix this?
Aucun commentaire:
Enregistrer un commentaire