gpt4 book ai didi

java - 解决java.lang.NumberFormatException : Invalid double: "" without losing placeholder text

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

onCreate 上初始化 EditTexts 会返回一个“”,它无法转换为有效的 double 值,因此会出现此错误。使用显式值可能会有所帮助,但随后我将丢失 android:hint 文本。
如何解决此问题而不丢失 android:hint 文本?

代码

public class MainActivity extends Activity {

private double quant, problem, verbal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




EditText quantScore = (EditText) findViewById(R.id.quant);
EditText problemScore = (EditText) findViewById(R.id.problem);
EditText verbalScore = (EditText) findViewById(R.id.verbal);
Button calculate = (Button) findViewById(R.id.button);



quant = Double.valueOf(quantScore.getText().toString());
problem = Double.valueOf(problemScore.getText().toString());
verbal = Double.valueOf(verbalScore.getText().toString());

final double total = quant + problem + verbal;




calculate.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "The total is " +total, Toast.LENGTH_LONG).show();
}
});




}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score To Percentile"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/quant"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:hint="Quantitative Ability"
android:gravity="center"
android:textStyle="italic" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/problem"
android:layout_marginTop="50dp"
android:layout_below="@+id/quant"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Problem Solving"
android:gravity="center"
android:textColorLink="#ffffdbeb"
android:textStyle="italic" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/verbal"
android:layout_marginTop="50dp"
android:hint="Verbal Ability"
android:gravity="center"
android:textColorLink="#ff48f3ff"
android:layout_below="@+id/problem"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="italic" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/button"
android:layout_below="@+id/verbal"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp"
android:textStyle="italic"
android:typeface="monospace" />

最佳答案

您应该捕获 NumberFormatException 并提供所需的默认值。

try {
quant = Double.valueOf(quantScore.getText().toString());
} catch(NumberFormatException e) {
quant = 0;
}

这适用于任何无法解析为 Double 的文本,而不仅仅是当 EditText 为空时。

关于java - 解决java.lang.NumberFormatException : Invalid double: "" without losing placeholder text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098971/

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