在没有重复Android游戏的活动之间洗牌

时间:2016-06-02 08:23:13

标签: android android-intent android-activity random shuffle

我有一个有许多不同活动的游戏。它从主菜单开始,然后Q1,Q2和Q3是包含内容的3个独立活动。我知道如何在活动之间进行洗牌,但总会有重复,因为我有大约30项活动。我一直在寻找和搜索,最后决定问你们。阅读有关捆绑包但无法自行解决的问题。我是个小伙子。除非我能看到代码,否则我会被卡住。我以不同的方式尝试自己,但我认为所有这些都是胡说八道。 所以基本上当它改组时它应该是Q1然后是Q3然后是Q4。 相反,因为它重复它有可能再次进入Q1然后Q3然后Q1再次,这对我的游戏来说是灾难性的。如果可能的话,请提供更详细的编码帮助。我知道我很讨厌,但我不知道我村里有其他程序员。

MainMenu.java

    public class MainMenu extends AppCompatActivity {

final ArrayList<Class> activities = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainmenu);

    activities.add(Q1.class);
    activities.add(Q2.class);
    activities.add(Q3.class);
}
public void onHintClick(View view) {

    Toast toasty = Toast.makeText(getApplicationContext(), getString(R.string.H1), Toast.LENGTH_SHORT);
    toasty.show();

}






public void onAnswerClick(View v){


    if (activities.size()==0){
        activities.add(Q1.class);
        activities.add(Q2.class);
        activities.add(Q3.class);

    }
    Collections.shuffle(activities);

    Class activityToShow = activities.get(0);

    Intent intent = new Intent(MainMenu.this, activityToShow);
    activities.remove(0);
    startActivity(intent);
       }
    }

Q1.java

    public class Q1 extends AppCompatActivity {

final ArrayList<Class> activities = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_q1);

    final ArrayList<Class> activities = new ArrayList<>();

    activities.add(Q2.class);
    activities.add(Q3.class);

}
    public void button(View view){


if (activities.size()==0){

    activities.add(Q2.class);
    activities.add(Q3.class);

}
Collections.shuffle(activities);

Class activityToShow = activities.get(0);

Intent intent = new Intent(Q1.this, activityToShow);
activities.remove(0);
startActivity(intent);
    }
    }

以及Q2和Q3的类似代码.....我需要知道的是,现在如何重复这些页面。请以最简单的方式告诉我,因为我真的是我的菜鸟。 提前谢谢!

Android Manifest

        package="com.test.cross">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:name=".MyApplication"

    android:theme="@style/AppTheme">


    <activity android:name=".MainMenu">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Q1" />
    <activity android:name=".Q2" />
    <activity android:name=".Q3">

    </activity>

   </application>


  </manifest> 

我的错误

    FATAL EXCEPTION: main
                                                                            Process: com.test.cross, PID: 2793
                                                                            java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                at android.view.View.performClick(View.java:5198)
                                                                                at android.view.View$PerformClick.run(View.java:21147)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.reflect.InvocationTargetException
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                at android.view.View.performClick(View.java:5198) 
                                                                                at android.view.View$PerformClick.run(View.java:21147) 
                                                                                at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                             Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                                at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                                at java.util.ArrayList.get(ArrayList.java:308)
                                                                                at com.test.cross.ActivityList.getRandomActivity(ActivityList.java:46)
                                                                                at com.test.cross.MainMenu.onAnswerClick(MainMenu.java:40)
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                at android.view.View.performClick(View.java:5198) 
                                                                                at android.view.View$PerformClick.run(View.java:21147) 
                                                                                at android.os.Handler.handleCallback(Handler.java:739) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   

1 个答案:

答案 0 :(得分:0)

您需要维护所有活动共有的活动列表,即ArrayList<Class> activities应该与Q1,Q2和Q3相同。您可以使用Application类通过实例化单例来实现相同的目的。

  1. 申请类 -

    public class MyApplication extends Application {
    
        @Override
        public void onCreate() {
            super.onCreate();
            ActivityList.init(30); // Pass number of activities here
        }
    }
    
  2. Singleton -

    public class ActivityList {
    
        private static ActivityList instance;
    
        private final ArrayList<Class> activities;
        private final int numActivities;
    
        public static void init(int numActivities) {
            if (instance == null)
                instance = new ActivityList(numActivities);
        }
    
        private ActivityList(int numActivities) {
            this.numActivities = numActivities;
            activities = new ArrayList<>(numActivities);
    
            try {
                for(int i = 1 ; i <= numActivities ; i++) {
                    activities.add(Class.forName("com.test.cross.Q" + i));
                }
            } catch(ClassNotFoundException e) {
                ...
            }
    
            Collections.shuffle(activities);
        }
    
        public static ActivityList getInstance() {
            return instance;
        }
    
        public Class getRandomActivity() {
            if (activities.isEmpty()) {
                return MainMenu.class;
            }
    
            Class clazz = activities.get(0);
            activities.remove(0);
    
            return clazz;
        }
    }
    
  3. 主菜单 -

    public class MainMenu extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mainmenu);
        }
    
        public void onHintClick(View view) {
            Toast toasty = Toast.makeText(this, getString(R.string.H1), Toast.LENGTH_SHORT);
            toasty.show();
        }
    
        public void onAnswerClick(View v) {
    
            Class activityToShow = ActivityList.getInstance().getRandomActivity();
    
            Intent intent = new Intent(MainMenu.this, activityToShow);
            startActivity(intent);
        }
    }
    
  4. 问题类 -

    public class Q1 extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_q1);
        }
    
        public void button(View view) {
    
            Class activityToShow = ActivityList.getInstance().getRandomActivity();
    
            Intent intent = new Intent(Q1.this, activityToShow);
            startActivity(intent);
        }
    }
    
  5. 在清单中,您需要指定您的Application类 -

    <application android:name=".MyApplication"
                 android:allowBackup="true"....>
        <activity .....>
    </application>
    

    编辑1:添加了一个循环来(重新)填充活动列表,所以如果你有多个类(Q1,Q2,....,Qn),你不需要手动添加所有这些。

    编辑2:添加了MainMenu课程。