java - 单击按钮时,应用程序被强制关闭

标签 java android android-activity random

如果单击按钮,我会将每个 Activity 设为随机,但似乎效果不佳,游戏被强制关闭。 “我使用 Bluestack 作为模拟器”

这是下面的代码

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);

        SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
        SharedPreferences.Editor editor = pref.edit();      
        editor.putInt("Lifes", 6);
        editor.putInt("Level", 0);  
        editor.putInt("One", 1);  
        editor.putInt("Two", 1);  
        editor.putInt("Three", 1);  
        editor.putInt("Four", 1); 
        editor.putInt("Five", 1);  


        editor.commit();


        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", 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: if(one == 1){
                 activity = activityone.class;
                 }

// if the activity was aready opened he will put value == 2, so the code will random again find activity with value == 1
                else if(one == 2){
                    Random generatorone = new Random();
                    number = generatorone.nextInt(5) + 1; 
                }

                 break;
             case 2: if(two== 1){
                 activity = activitytwo.class;
                 }
                else if(two== 2){
                    Random generatortwo = new Random();
                    number = generatortwo.nextInt(5) + 1; 
                }
                 break;
             case 3:if(three== 1){
                 activity = activitythree.class;
                 }
                else if(three== 2){
                    Random generatorthree = new Random();
                    number = generatorthree.nextInt(5) + 1; 
                }
                 break;
             case 4:if(four == 1){
                 activity = activityfour.class;
                 }
                else if(four == 2){
                    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);
  }

但是当我单击按钮时,应用程序被强制关闭。我错过了什么吗?有人可以帮忙吗?

在每个 Activity 中都有一个代码来更新值==2

          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);  
                                        editor.putInt("One", 2);
                                        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: if(one == 1){
                                                activity = activityone.class;
                                                }
                                               else if(one == 2){
                                                   Random generatorone = new Random();
                                                   number = generatorone.nextInt(5) + 1; 
                                               }
                                                break;
                                            case 2: if(two== 1){
                                                activity = activitytwo.class;
                                                }
                                               else if(two== 2){
                                                   Random generatortwo = new Random();
                                                   number = generatortwo.nextInt(5) + 1; 
                                               }
                                                break;
                                            case 3:if(three== 1){
                                                activity = activitythree.class;
                                                }
                                               else if(three== 2){
                                                   Random generatorthree = new Random();
                                                   number = generatorthree.nextInt(5) + 1; 
                                               }
                                                break;
                                            case 4:if(four == 1){
                                                activity = activityfour.class;
                                                }
                                               else if(four == 2){
                                                   Random generatorFour = new Random();
                                                   number = generatorFour.nextInt(5) + 1; 
                                               }    
                                            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);
                 }                
         });
}

最佳答案

您没有将值分配给一、二..并且每次这些值都为零。因此,如果总是将 Activity 设为空..那么您需要将值分配给变量一、二等.. ..

switch(number) { 
         // E.g., if the output is 1, the activity we will open is ActivityOne.class


             case 1: if(one == 1){
                 activity = activityone.class;
                 }

// if the activity was aready opened he will put value == 2, so the code will random again find activity with value == 1
                else if(one == 2){
                    Random generatorone = new Random();
                    number = generatorone.nextInt(5) + 1; 
                }

在上面的代码中,您正在分配 Activity 对象。一查我就觉得是问题!

关于java - 单击按钮时,应用程序被强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32285979/

相关文章:

java - 如何设置 Spring Context 来恢复/处理临时资源故障

java - 需要返回字符串,但只能使用随机数生成器获取 int

android - 未调用 Firebase onDataChange

android - ActionBar 在按下时更改菜单项按钮的颜色

android - 检查文档是否存在

android - 从 Android 库项目覆盖 Activity

java - Android Studio appengine 端点不包括构建器

java - 从 JFormattedTextField 解析 double

android - 如何在 android 上显示当前可见 Activity 的对话框?

android - 如何在运行时设置应用程序的入口 Activity ?