在模拟器

时间:2016-10-14 10:15:27

标签: java android android-studio math

我目前正在Android Studio的一个学校项目上工作,到目前为止,我已经写过(在stackoverflow.com的帮助下)一个算法,每次按下按钮时都会产生随机方程式屏幕。然后,等式显示在屏幕上的文本视图中。我添加了第二个文本视图,其中显示了等式的结果。 到目前为止,这是我的代码:

String[] operationSet = new String[]{"+", "-", "/", "*"};

public void generate(View view) {
    Random random = new Random();
    int numOfOperations = random.nextInt(2) + 1;

    List<String> operations = new ArrayList<>();

    for (int i = 0; i < numOfOperations; i++) {
        String operation = operationSet[random.nextInt(4)];
        operations.add(operation);
    }

    int numOfNumbers = numOfOperations + 1;
    List<Integer> numbers = new ArrayList<>();

    for (int i = 0; i < numOfNumbers; i++) {
        int number = random.nextInt(10)+1;
        numbers.add(number);
    }

    String equation = "";
    for (int i = 0; i < numOfOperations; i++) {
        equation += numbers.get(i);
        equation += operations.get(i);
    }
    equation += numbers.get(numbers.size() -1);

    TextView TextEquation = (TextView)findViewById(R.id.textView);
    TextEquation.setText(equation);

    String stringResultOfEquation = String.valueOf(equation);

    // Calculate the result of the equation

    double doubleEquation = Double.parseDouble(equation);
    double doubleResult = abs(doubleEquation);
    String stringResult = String.valueOf(doubleResult);

    TextView textResult = (TextView)findViewById(R.id.textView2);
    textResult.setText(stringResult);

}

现在的问题是,当我在模拟器中启动应用程序并按下屏幕上的按钮时,我的应用程序崩溃并显示错误消息:&#34;不幸的是SolveIt已停止&#34; 一切都运行得很好,直到我添加了第二个textview2及其中的等式结果。所以我猜它肯定有问题:

double doubleEquation = Double.parseDouble(equation);
double doubleResult = abs(doubleEquation);
String stringResult = String.valueOf(doubleResult);

TextView textResult = (TextView)findViewById(R.id.textView2);
textResult.setText(stringResult);

将字符串转换为双字母时可能存在问题,但我真的不知道。

如果有人想知道这是logcat:

10-12 22:41:23.646 14905-14905/mycompany.solveit E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: mycompany.solveit, PID: 14905
                                                               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:4780)
                                                                   at android.view.View$PerformClick.run(View.java:19866)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                   at android.view.View.performClick(View.java:4780) 
                                                                   at android.view.View$PerformClick.run(View.java:19866) 
                                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                                                                Caused by: java.lang.NumberFormatException: Invalid double: "3/1"
                                                                   at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                   at java.lang.StringToReal.initialParse(StringToReal.java:164)
                                                                   at java.lang.StringToReal.parseDouble(StringToReal.java:282)
                                                                   at java.lang.Double.parseDouble(Double.java:301)
                                                                   at mycompany.solveit.FullscreenActivity.generate(FullscreenActivity.java:210)
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                   at android.view.View.performClick(View.java:4780) 
                                                                   at android.view.View$PerformClick.run(View.java:19866) 
                                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                   at android.os.Looper.loop(Looper.java:135) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

10-12 22:41:25.672 14905-14905 / mycompany.solveit I / Process:发送信号。 PID:14905 SIG:9

如果我的问题中有任何不明确的地方,请随时提出,我会尽力澄清问题:)

提前谢谢你!

0 个答案:

没有答案