执行setText()时如何修复致命错误

时间:2019-06-23 03:16:59

标签: java android android-studio

当我执行onClick方法时,应用程序崩溃。问题似乎出在setText函数中

我已将问题缩小为给定的代码:

在检测到的代码中没有语法错误 Java:

public void calculate(View view){

        double subTotal;
        double Total;
        double tipAmount;
        TextView showTotal = findViewById(R.id.textView2);
        TextView tip = findViewById(R.id.Tip);
            SeekBar percent = findViewById(R.id.seekBar);
            int percent1 = percent.getProgress() + 100;
            EditText input = findViewById(R.id.editText);
            String placeHolder = input.getText().toString();
            subTotal = Double.parseDouble(placeHolder);

            if ((subTotal >= 0) && (subTotal <= 999999) && (subTotal == (double)subTotal)){
            Total = Double.parseDouble(roundTwo.format(subTotal * percent1 * .01));
            tipAmount = Double.parseDouble(roundTwo.format(subTotal * (percent1-100) *.01));
            String totalString = Double.toString(Total);
            tip.setText(getResources().getString(R.string.Tip_Amount, tipAmount));
            showTotal.setText(getResources().getString(R.string.Total_Amount,Total));



        }
            else {
                showTotal.setText(getResources().getString(R.string.error));
            }
    }

字符串XML:

<string name="Tip_Amount">Tip: %1$d</string>
<string name="Total_Amount">Total: %1$d</string>

由于Java中给定的行,应用程序崩溃。 这是Logcat数据: Logcat:

 Process: com.example.tipcalculator, PID: 19871
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:7251)
        at android.view.View.performClickInternal(View.java:7228)
        at android.view.View.access$3500(View.java:802)
        at android.view.View$PerformClick.run(View.java:27843)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7116)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7251) 
        at android.view.View.performClickInternal(View.java:7228) 
        at android.view.View.access$3500(View.java:802) 
        at android.view.View$PerformClick.run(View.java:27843) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7116) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925) 
     Caused by: java.util.IllegalFormatConversionException: d != java.lang.Double
        at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4403)
        at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2885)
        at java.util.Formatter$FormatSpecifier.print(Formatter.java:2839)
        at java.util.Formatter.format(Formatter.java:2530)
        at java.util.Formatter.format(Formatter.java:2459)
        at java.lang.String.format(String.java:2911)
        at android.content.res.Resources.getString(Resources.java:485)
        at com.example.tipcalculator.MainActivity.calculate(MainActivity.java:88)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
        at android.view.View.performClick(View.java:7251) 
        at android.view.View.performClickInternal(View.java:7228) 
        at android.view.View.access$3500(View.java:802) 
        at android.view.View$PerformClick.run(View.java:27843) 
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7116) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925) 

2 个答案:

答案 0 :(得分:2)

我认为您通过tipAmountTotal传递的内容存在类型问题,因为您的字符串格式化程序%1$d 期望int 不为double类型或其他类型。首先,尝试使用Log打印它们。

这将起作用

tip.setText(getResources().getString(R.string.Tip_Amount, 100));
showTotal.setText(getResources().getString(R.string.Total_Amount, 1000));

这不起作用

tip.setText(getResources().getString(R.string.Tip_Amount, 100.00))
showTotal.setText(getResources().getString(R.string.Total_Amount, 1000.00));

请点击链接以了解有关字符串格式Formatter

的更多信息

答案 1 :(得分:0)

您在strings.xml的d中使用了%1$d,因此它需要一个整数。但是,您正在发送一个双精度,因此会发生错误。由于您需要小数部分,因此可以使用f代替d并按如下所示更新strings.xml:

<string name="Tip_Amount">Tip: %1$.2f</string>
<string name="Total_Amount">Total: %1$.2f</string>

这样,您还会在点后两位数字处得到一个很好的表示。我建议顺便说一下,将BigDecimal用于货币。