【问题标题】:Android: Matching string to Color within a Hashset- displaying randomly on screen?Android:将字符串与哈希集中的颜色匹配 - 在屏幕上随机显示?
【发布时间】:2014-07-27 21:39:20
【问题描述】:

我正在尝试创建一个类似于STROOP EFFECT 的安卓游戏。

在我的第一步中,我尝试创建一个 HashMap,其中颜色字符串作为键,其对应的 android 颜色作为值。即:

HashMap<String, Integer> colors= new HashMap<>();

        colors.put("Green", Color.GREEN);
        colors.put("Blue", Color.BLUE);
        colors.put("Red", Color.RED);
        colors.put("Yellow", Color.YELLOW);
        colors.put("Black", Color.BLACK);

然后我将键集和值都转换为数组。 (见下面的代码)

我在实现时遇到问题的是设置 Textview(将显示字符串)以显示数组中的随机字符串,并将此字符串设置为颜色数组中的随机颜色。当我尝试让我的应用程序崩溃时,我收到以下错误:

07-27 22:36:00.192: E/AndroidRuntime(32079): FATAL EXCEPTION: main
07-27 22:36:00.192: E/AndroidRuntime(32079): Process: com.example.brianapp, PID: 32079
07-27 22:36:00.192: E/AndroidRuntime(32079): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.Stroop}: java.lang.ArrayIndexOutOfBoundsException: length=0; index=2
07-27 22:36:00.192: E/AndroidRuntime(32079):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
07-27 22:36:00.192: E/AndroidRuntime(32079):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)

当前活动代码:

public class Stroop extends ActionBarActivity {

    HashMap<String, Integer> colors= new HashMap<>();
    //putting the strings of the hasmap to an array
    Object stringOnScreen[]= colors.keySet().toArray();

    Object colorsOnScreen[]= colors.values().toArray();


    TextView color;
    Button btn1;
    Button btn2;

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

        Log.d("test", "test: " + stringOnScreen[2].toString()); //trace code


    }//oncreate end


    public void setUpQuestion(){

        //set the text of the string in textview for user to see
        color.setText("" + stringOnScreen[randInt(0, 4)]);

        color.setTextColor((int) colorsOnScreen[randInt(0,4)]);




    }





    public void setUpGame(){

        // setting up the hashmap
        colors.put("Green", Color.GREEN);
        colors.put("Blue", Color.BLUE);
        colors.put("Red", Color.RED);
        colors.put("Yellow", Color.YELLOW);
        colors.put("Black", Color.BLACK);

        // setting up vars
        color = (TextView) findViewById(R.id.tvStroopColor);
        btn1 = (Button) findViewById(R.id.btnStroop1);
        btn2 = (Button) findViewById(R.id.btnStroop2);

    }

    /**
     * method to get a random int
     * @param min
     * @param max
     * @return
     */
    public static int randInt(int min, int max) {

        // NOTE: Usually this should be a field rather than a method
        // variable so that it is not re-seeded every call.
        Random rand = new Random();

        // nextInt is normally exclusive of the top value,
        // so add 1 to make it inclusive
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }


}

如何实现此功能?任何帮助都会很棒!

尝试的解决方案:

public class Stroop extends ActionBarActivity {

    HashMap<String, Integer> colors= new HashMap<>();
    //putting the strings of the hahsmap to an array
    Object stringOnScreen[]= colors.keySet().toArray();

    Object colorsOnScreen[]= colors.values().toArray();


    TextView color;
    Button btn1;
    Button btn2;

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

        setUpGame();

        setUpQuestion();



    }//oncreate end


    public void setUpQuestion(){

        int randString= new Random().nextInt(stringOnScreen.length);
        int randColor= new Random().nextInt(colorsOnScreen.length);


        //set the text of the string in textview for user to see
        color.setText("" + stringOnScreen[randString]);

        color.setTextColor(randColor);



    }


    public void setUpGame(){

        // setting up the hashmap
        colors.put("Green", Color.GREEN);
        colors.put("Blue", Color.BLUE);
        colors.put("Red", Color.RED);
        colors.put("Yellow", Color.YELLOW);
        colors.put("Black", Color.BLACK);

        // setting up vars
        color = (TextView) findViewById(R.id.tvStroopColor);
        btn1 = (Button) findViewById(R.id.btnStroop1);
        btn2 = (Button) findViewById(R.id.btnStroop2);

    }




}

现在得到以下 Logcat 错误报告:

07-27 23:11:22.482: E/AndroidRuntime(11902): FATAL EXCEPTION: main
07-27 23:11:22.482: E/AndroidRuntime(11902): Process: com.example.brianapp, PID: 11902
07-27 23:11:22.482: E/AndroidRuntime(11902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brianapp/com.example.brianapp.Stroop}: java.lang.IllegalArgumentException: n <= 0: 0
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.os.Looper.loop(Looper.java:157)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread.main(ActivityThread.java:5356)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at java.lang.reflect.Method.invoke(Method.java:515)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at dalvik.system.NativeStart.main(Native Method)
07-27 23:11:22.482: E/AndroidRuntime(11902): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
07-27 23:11:22.482: E/AndroidRuntime(11902):    at java.util.Random.nextInt(Random.java:175)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at com.example.brianapp.Stroop.setUpQuestion(Stroop.java:51)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at com.example.brianapp.Stroop.onCreate(Stroop.java:42)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.Activity.performCreate(Activity.java:5426)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-27 23:11:22.482: E/AndroidRuntime(11902):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
07-27 23:11:22.482: E/AndroidRuntime(11902):    ... 11 more

注释行:

 07-27 23:11:22.482: E/AndroidRuntime(11902): Caused by: java.lang.IllegalArgumentException: n <= 0: 0
    07-27 23:11:22.482: E/AndroidRuntime(11902):    at java.util.Random.nextInt(Random.java:175)
    07-27 23:11:22.482: E/AndroidRuntime(11902):    at com.example.brianapp.Stroop.setUpQuestion(Stroop.java:51)
    07-27 23:11:22.482: E/AndroidRuntime(11902):    at 

【问题讨论】:

    标签: java android arrays hashset


    【解决方案1】:

    正如您的日志所说,您的数组索引超出了范围,因为您的数组为空。在调用setUpGame 之后初始化stringOnScreencolorOnScreen,并在访问数组之前完成这两项操作。

    public class Stroop extends ActionBarActivity {
    
        HashMap<String, Integer> colors = new HashMap<>();
    
        Object stringOnScreen[];
        Object colorsOnScreen[];
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.stroop);
    
            setUpGame()
    
            stringOnScreen = colors.keySet().toArray();
            colorOnScreen = colors.values().toArray();
    
            Log.d("test", "test: " + stringOnScreen[2].toString());
    
        }
    
        ...
    }
    

    【讨论】:

    • 我尝试在考虑您的解决方案的情况下解决问题。请查看我最近的编辑,因为我现在遇到了不同的问题
    • 您在构建时仍在初始化您的字段,因此它们仍然是空的。再看看我的解决方案。您需要在调用setUpGame 之后初始化stringOnScreencolorOnScreen
    猜你喜欢
    • 2015-10-31
    • 1970-01-01
    • 2017-03-19
    • 2012-03-10
    • 2017-05-28
    • 2013-03-11
    • 2016-05-29
    • 2017-06-04
    • 2012-05-16
    相关资源
    最近更新 更多