在不重复的活动之间随机播放 Android 游戏

shuffle between activities without repetition Android game

我有一款游戏有很多不同的活动。它从主菜单开始,然后是 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 清单

        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)   

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

  1. 申请class -

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

    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. 问题classes -

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

在您的清单中,您需要指定您的应用程序 class -

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

编辑 1: 添加了一个循环来(重新)填充活动列表,因此如果您有多个 classes(Q1,Q2,.. ..,Qn),不需要全部手动添加。

编辑 2: 添加 MainMenu class.