Java 如何从EditText检查NullPointerException
我想计算平均成绩,现在我有了基础知识和布局。当Java 如何从EditText检查NullPointerException,java,android,nullpointerexception,android-edittext,Java,Android,Nullpointerexception,Android Edittext,我想计算平均成绩,现在我有了基础知识和布局。当EditText处于打开状态时,它会抛出一个NullPointerException,我知道它是什么,但不知道如何用代码检查/处理它。有人能告诉我怎么做吗 欢迎任何提示 final TextView averageView = findViewById(R.id.averageView); final String averageText = getString(R.string.average); final Button calculateB
EditText
处于打开状态时,它会抛出一个NullPointerException
,我知道它是什么,但不知道如何用代码检查/处理它。有人能告诉我怎么做吗
欢迎任何提示
final TextView averageView = findViewById(R.id.averageView);
final String averageText = getString(R.string.average);
final Button calculateButton = findViewById(R.id.calculateAverageButton);
final Button addGradeButton = findViewById(R.id.addGradeButton)
calaculateButton.setOnClickListener(new View.onClickListener() {
@SupressLint("SetTextI18n")
public void onClick(View v) {
double grade[] = {Double.parseDouble(((EditText) findViewById(R.id.grade1)).getText().toString());
double weight[] = {Double.parseDouble(((EditText) findViewById(R.id.weight1)).getText().toString());
double weightTotal = weight[0]; double sum = grade[0] * weight[0]
double average = sum / weightTotal
@SuppressLint("DefaultLocale") String averageResult = String.format(%.2f, average);
averageView.setText(averageText + " " + averageResult);
您需要检查编辑文本是否为空,您可以执行以下操作:
private String getGrade1() {
if ((EditText) findViewById(R.id.grade1)).getText().toString() != null ||
(EditText)findViewById(R.id.grade1)).getText().toString() != "") {
return (EditText) findViewById(R.id.grade1)).getText().toString();
} else {
//show a toast saying the the user needs to fill this input
}
private boolean isDouble(String value) {
try {
Double.parseDouble(value);
return true;
} catch (Exception ex) {
return false;
}
}
您可以在解析textview之前简单地检查它的文本是否为null,然后正确地处理它
TextView tvGrade1 = findViewById(R.id.grade1));
TextView tvWeight1 = findViewById(R.id.weight1));
if(tvGrade1.getText() == null || tvWeight1.getText() == null)
//one of them is null, do stuff
因此,在调用可能为null的“toString()”时必须小心
反对
特别是如果你打算把它投给替身
注意*您的代码还从中的单个条目创建了一个双精度数组
编辑文本,以便在强制转换之前对文本格式进行额外检查
也应该这样做,以确保它是双重的。你可以采取懒散的方式,用try/catch来包装它
如果您愿意,告诉他们它的格式不正确
处理数字格式异常意味着您需要在使用它之前确认它是双精度的。您可以使用一个很好的实用程序,也可以编写一个简单的助手方法。Guava有一个helper方法,Apache有helper方法,或者您可以像这里这样编写正则表达式模式匹配器。regex的好处在于,您不依赖try/catch来识别问题
但为了彻底起见,你可以这样做:
private String getGrade1() {
if ((EditText) findViewById(R.id.grade1)).getText().toString() != null ||
(EditText)findViewById(R.id.grade1)).getText().toString() != "") {
return (EditText) findViewById(R.id.grade1)).getText().toString();
} else {
//show a toast saying the the user needs to fill this input
}
private boolean isDouble(String value) {
try {
Double.parseDouble(value);
return true;
} catch (Exception ex) {
return false;
}
}
您有几个选项来处理null
1) 在“if/else”语句中检查null
double grade[];
if (myText.getText() != null && isDouble(myText.getText().toString()) {
grade = {Double.parseDouble(myText.getText().toString())}
} else {
//it's null, don't use it
}
2) 检查是否为空内联
if (isDouble(myText.getText().toString()) {
double grade[] = myText.getText() == null ? {0.0, 0.0} : {Double.parseDouble(myText.getText().toString())}
}
3) 允许null作为选项
double grade[] = myText.getText() == null ? null : {Double.parseDouble(myText.getText().toString())}
4) 作弊并使用Try/Catch(如果您不想掌握最好的处理方法,只想确保它不会崩溃,并且可以将数据格式问题通知用户,您可以简单地将Try/Catch包装在所有代码周围,并说“格式不正确”每一次,但这不是非常描述性的,可能没有足够的帮助,仅供参考
5) 或者,如果你愿意使用Kotlin,你可以为每一种方法都提供很好的扩展方法
fun EditText.doubleArrayOrDefault(): ArrayList<Double> {
return if (this.getText() != null && isDouble(myText.getText().toString())) {
{Double.parseDouble(myText.getText().toString())}
} else {
{0.0, 0.0} // or whatever you want your default to be
}
}
在XML中,您可以将其更新为
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<data>
<variable name="activity" type="com.package.a35.activities.MyActivity" />
</data>
<EditText
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={activity.myObservableText}" />
当然,如果您想编写一个非常简单的适配器,还可以向适配器添加JVMStatic来处理与double数组的绑定
当然,这都是伪的,不是在实际的IDE中键入的,但它将非常接近,所以希望这将有助于您的选项
祝你好运,希望其中一个选项能很好地满足你的需要
愉快的编码。您也可以简单地将整个代码用onClick()方法包装在try-catch语句中,然后捕获异常并执行必要的任务,或者向用户显示toast错误消息,说明他没有在这两个字段中输入值。我们需要stacktrace。我目前没有访问stacktrace的权限,但是错误是我从EditText中获取文本,但是没有什么可获取的。(NullPointerException)作为其空。我正在寻找一种方法,如何检查空/空值等。不需要stacktrace,如果尝试对空值执行toString(),将导致异常。你的问题提供了足够的数据来帮助你。请看我的答案。可能是hi的副本,谢谢你的精彩解释。我使用了if/else语句,但现在出现了错误
java.lang.NumberFormatException:empty string
现在我不知道该怎么办。@J.doe,如果(myText.getText()!=null&!myText.getText().toString().isEmpty())添加此条件
然后给它try.J.doe我在Note*部分提到过,您需要确认格式。我在选项4中也提到过,您可以使用try-catch进行包装。我将进一步更新以帮助您您永远不要捕获NullPointerException
。