是什么导致我的应用程序在此程序中崩溃? - Android Studio

时间:2016-10-24 09:46:40

标签: java android android-studio

我编写了一个程序,其中生成随机数并在屏幕上显示,显示它们被加在一起。然后,用户必须输入两个或更多个数字的总和来得分。在下面的代码中都清楚地表明了这一点。我的问题是当按下开始游戏的按钮时,我的应用程序崩溃/我以前没有遇到过这个问题,虽然程序还没有完全完成,所以我不得不进一步开发它。在这个开发中,我遇到了应用程序崩溃的问题。我相信这与loadG2a和loadG2b随机int变量有关,可能要么将它们集成到parseInt中(numbers.getText()),要么将它们一起设置在'数字中。 TextView在第一位。可能根本不是这些。无论哪种方式,我都试图解决这个问题,但未找到修复方法。这是代码:

final Button loseStarter2;

        loseStarter2 = (Button) findViewById(R.id.Starter2);
        loseStarter2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loseStarter2.setVisibility(View.GONE);
                infoG2.setVisibility(View.GONE);

                final Random generateG2 = new Random();
                final int loadG2a = generateG2.nextInt(89)+10;
                final int loadG2b = generateG2.nextInt(89)+10;
                final int loadG2c = 0;
                final int loadG2d = 0;
                final TextView numbers = (TextView) findViewById(R.id.numbers);
                numbers.setText(loadG2a+loadG2b);
                final EditText input2 = (EditText) findViewById(R.id.input2);
                if (true) {
                    input2.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                switch (keyCode) {
                                    case KeyEvent.KEYCODE_ENTER:
                                        Editable answer2 = input2.getText();
                                        int finalAnswer2 = Integer.parseInt(String.valueOf(answer2));
                                        int finalLoadG2 = Integer.parseInt(String.valueOf(numbers.getText()));
                                        input2.getText().clear();

                                        if (finalAnswer2 == finalLoadG2) {
                                            score++;
                                        }


                                        if (score>=0 && score<=3){
                                            int loadG2a = generateG2.nextInt(89)+10;
                                            int loadG2b = generateG2.nextInt(89)+10;
                                            int loadG2c = 0;
                                            int loadG2d = 0;
                                            numbers.setText(loadG2a+loadG2b);

                                        }

                                        if (score>=4 && score<=6){
                                            int loadG2a = generateG2.nextInt(899)+100;
                                            int loadG2b = generateG2.nextInt(899)+100;
                                            int loadG2c = 0;
                                            int loadG2d = 0;
                                            numbers.setText(loadG2a+loadG2b);
                                        }

                                        if (score>=7 && score<=9){
                                            int loadG2a = generateG2.nextInt(89)+10;
                                            int loadG2b = generateG2.nextInt(89)+10;
                                            int loadG2c = generateG2.nextInt(89)+10;
                                            int loadG2d = 0;
                                            numbers.setText(loadG2a+loadG2b+loadG2c);
                                        }

                                        if (score>=10 && score<=12){
                                            int loadG2a = generateG2.nextInt(899)+100;
                                            int loadG2b = generateG2.nextInt(899)+100;
                                            int loadG2c = generateG2.nextInt(899)+100;
                                            int loadG2d = 0;
                                            numbers.setText(loadG2a+loadG2b+loadG2c);
                                        }

                                        if (score>=13 && score<=15){
                                            int loadG2a = generateG2.nextInt(89)+10;
                                            int loadG2b = generateG2.nextInt(89)+10;
                                            int loadG2c = generateG2.nextInt(89)+10;
                                            int loadG2d = generateG2.nextInt(89)+10;
                                            numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
                                        }

                                        if (score>=16 && score<=18){
                                            int loadG2a = generateG2.nextInt(899)+100;
                                            int loadG2b = generateG2.nextInt(899)+100;
                                            int loadG2c = generateG2.nextInt(899)+100;
                                            int loadG2d = generateG2.nextInt(899)+100;
                                            numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
                                        }

                                        if (score>=19 && score<=20){
                                            int loadG2a = generateG2.nextInt(8999)+1000;
                                            int loadG2b = generateG2.nextInt(8999)+1000;
                                            int loadG2c = generateG2.nextInt(8999)+1000;
                                            int loadG2d = 0;
                                            numbers.setText(loadG2a+loadG2b+loadG2c);
                                        }

                                        if (score>=21 && score<=22){
                                            int loadG2a = generateG2.nextInt(8999)+1000;
                                            int loadG2b = generateG2.nextInt(8999)+1000;
                                            int loadG2c = generateG2.nextInt(8999)+1000;
                                            int loadG2d = generateG2.nextInt(8999)+1000;
                                            numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
                                        }

                                        if (score>=23){
                                            int loadG2a = generateG2.nextInt(89999)+10000;
                                            int loadG2b = generateG2.nextInt(89999)+10000;
                                            int loadG2c = generateG2.nextInt(89999)+10000;
                                            int loadG2d = generateG2.nextInt(89999)+10000;
                                            numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d);
                                        }


                                }
                            }

                            return false;
                        }
                    });
                }

我很感激愿意帮助我解决这个问题的人,让程序以预期的方式运行。请随意替换任何必要的代码。非常感谢提前。

1 个答案:

答案 0 :(得分:1)

// here is int's (all other lines with int's must be also corrected)
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d); 

// but must be String =)
numbers.setText(loadG2a+loadG2b+loadG2c+loadG2d+"");