从另一个函数调用函数后,应用程序停止工作

时间:2017-02-26 01:04:42

标签: java android android-studio

我正在创建一种大脑训练器应用程序,仅用于练习。有两个数字被添加或减去,我们需要猜测结果。有四个按钮,一个是正确的值,另外三个是随机数。我有一个函数setQuestion()来完成所有这些。 当我按下其中一些按钮时,它会检查数字是否正确并更新分数。它应该调用函数setQuestion(),以便创建新值,但由于某种原因,单击按钮后应用程序崩溃。

setQuestion函数有效,因为它是在打开应用程序时运行的。 这是这两个函数的代码。

public void setQuestion(){
    int firstNumber = randomNumberGenerator.nextInt(101);
    int secondNumber = randomNumberGenerator.nextInt(101);
    int operation = randomNumberGenerator.nextInt(2);
    TextView firstNumberTextView = (TextView) findViewById(R.id.firstNumber);
    TextView secondNumberTextView = (TextView) findViewById(R.id.secondNumber);
    TextView operationTextView = (TextView) findViewById(R.id.operation);
    if(operation == 0){
        sum = firstNumber + secondNumber;
        operationTextView.setText("+");
    }
    else{
        while (secondNumber > firstNumber){
            secondNumber = randomNumberGenerator.nextInt(101);
        }
        operationTextView.setText("-");
        sum = firstNumber - secondNumber;
    }
    firstNumberTextView.setText(String.valueOf(firstNumber));
    secondNumberTextView.setText(String.valueOf(secondNumber));
    int operation1 = randomNumberGenerator.nextInt(2);
    int operation2 = randomNumberGenerator.nextInt(2);
    int operation3 = randomNumberGenerator.nextInt(2);
    int answer1 = 0;
    int answer2 = 0;
    int answer3 = 0;
    switch (operation1){
        case 0:
            answer1 = sum + randomNumberGenerator.nextInt(9) +1 +1;
        break;
        case 1:
            int temp = randomNumberGenerator.nextInt(9) +1;
            if(sum > temp){
                answer1 = sum - temp;
            }
            else{
                answer1 = temp;
            }

    }

    switch (operation2){
        case 0:
            answer2 = sum + randomNumberGenerator.nextInt(9) +1;
            break;
        case 1:
            int temp = randomNumberGenerator.nextInt(9) +1;
            if(sum > temp){
                answer2 = sum - temp;
            }
            else{
                answer2 = temp;
            }

    }

    switch (operation3){
        case 0:
            answer3 = sum + randomNumberGenerator.nextInt(9) +1;
            break;
        case 1:
            int temp = randomNumberGenerator.nextInt(9) +1;
            if(sum > temp){
                answer3 = sum - temp;
            }
            else{
                answer3 = temp;
            }

    }
    buttonMap.add(0);
    buttonMap.add(1);
    buttonMap.add(2);
    buttonMap.add(3);
    Collections.shuffle(buttonMap);

    int[] valArray = {sum, answer1, answer2, answer3};

    for (int i = 0; i < buttonMap.size(); i++) {
        int id = getResources().getIdentifier("b"+buttonMap.get(i), "id", getPackageName());
        Button button = (Button) findViewById(id);
        button.setText(String.valueOf(valArray[i]));
    }




}

另一个:

public void answer(View view){
    Button clickedButton = (Button) view;
    String clickedValue = clickedButton.getText().toString();
    int clickedValueInt = Integer.parseInt(clickedValue);
    TextView allQuestion = (TextView) findViewById(R.id.totalQuestions);
    numberOfQuestions = Integer.parseInt(allQuestion.getText().toString());
    TextView correctQuestions = (TextView) findViewById(R.id.correctQuestions);
    numberOfCorrectQuestions = Integer.parseInt(correctQuestions.getText().toString());
    if(clickedValueInt == sum){
        numberOfCorrectQuestions++;
        correctQuestions.setText(String.valueOf(numberOfCorrectQuestions));
    }
    System.out.println(numberOfCorrectQuestions);
    numberOfQuestions++;
    allQuestion.setText(String.valueOf(numberOfQuestions));
    setQuestion();
}

这是来自logcat的文本。

02-26 02:34:19.965 305-305 /? E / lowmemorykiller:编写错误/ proc / 18289 / oom_score_adj;错误号= 22 02-26 02:34:19.987 850-6076 /? E / ReportTools:这不是beta用户构建 02-26 02:34:20.167 850-1922 /? E / HsmCoreServiceImpl:代码中的onTransact是:102 02-26 02:34:20.221 27755-27832 /? E / PackageLogInfoManager:checkPackageLogState,cr:android.app.ContextImpl$ApplicationContentResolver@bb1c2fa,packageNames:null 02-26 02:34:20.327 18468-18486 /? E / linker:readlink(&#39; / proc / self / fd / 23&#39;)失败:权限被拒绝[fd = 23] 02-26 02:34:20.328 18468-18486 /? E / linker:警告:无法获取库的实际路径&#34; /system/lib64/hw/gralloc.hi6250.so"。将使用给定的名称。 02-26 02:34:20.335 18468-18486 /? E / linker:readlink(&#39; / proc / self / fd / 23&#39;)失败:权限被拒绝[fd = 23] 02-26 02:34:20.335 18468-18486 /? E / linker:警告:无法获取库的实际路径&#34; libion​​.so&#34;。将使用给定的名称。 02-26 02:34:20.461 18468-18468 /? E / TotemWeather:Utils错误:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.463 18468-18485 /? E / TotemWeather:Utils错误:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.522 9539-9608 /? E / HwLauncher:SettingsEx,没有这样的字段。 02-26 02:34:20.525 18468-18468 /? E / TotemWeather:Utils错误:getWidgetShowCityInfo返回myLocation 02-26 02:34:20.929 1854-1854 /? E / RegisteredServicesCache:invalidateCache set mNeedToastTableFull

2 个答案:

答案 0 :(得分:0)

检查TextViews中的所有文本是否都可以转换为整数。你可以在你的问题中添加堆栈跟踪吗?

答案 1 :(得分:0)

错误就是这样。我有一个名为buttonMap的arrayList,我用它来访问按钮。每次调用setQuestion()方法时,它都会添加4个值(0,1,2,3)。我刚刚删除了这段代码

buttonMap.add(0);
    buttonMap.add(1);
    buttonMap.add(2);
    buttonMap.add(3);
从该方法

一切正常。

相关问题