Android:在Hashset中将字符串与Color匹配 - 在屏幕上随机显示?

时间:2014-07-27 21:39:20

标签: java android arrays hashset

我正在尝试创建一个类似于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(将显示字符串的位置)以显示数组中的随机String,并将此字符串设置为颜色数组中的随机颜色。当我尝试我的应用程序崩溃时,我收到以下错误:

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 

1 个答案:

答案 0 :(得分:0)

正如您的日志所说,您的数组索引超出范围,因为您的数组是空的。在调用stringOnScreen之后初始化colorOnScreensetUpGame并在访问阵列之前执行这两项操作。

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

    }

    ...
}