gpt4 book ai didi

java - 如何修复执行 setText() 时的 fatal error

转载 作者:行者123 更新时间:2023-12-02 01:27:18 25 4
gpt4 key购买 nike

当我执行 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.util.concurrent.java 文件中的给定行而崩溃。这是 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) 

最佳答案

我认为您作为 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

关于java - 如何修复执行 setText() 时的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56720734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com