【发布时间】:2022-01-11 22:58:57
【问题描述】:
为什么我无法将 String(取自 EditText)转换为 Float。
我正在构建一个简单的应用程序来根据无线电组选择计算(加法、减法、除法、乘法)两个数字。 但是,当我使用
提取字符串时String temp = editText.getText().toString();
然后尝试将其转换回Float以便对其执行操作
num1 = Float.parseFloat(temp);
这行代码会导致一些错误,导致我的应用程序崩溃。 我试图用 try-catch 将其包围,以防止我的应用程序崩溃,但 String 仍未解析为 Float。
计算器应用程序的完整 Java 代码
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
float ans;
EditText editText;
TextView textView;
float num1, num2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.first_number);
String temp = editText.getText().toString();
try{
num1 = Float.parseFloat(temp);
}catch(Exception e){
Log.e("MainActivity", "onCreate: Still Not Working", e);
}
editText = findViewById(R.id.second_number);
try{
num2 = Float.parseFloat(editText.getText().toString());
}catch(Exception e){
Log.e("MainActivity", "onCreate: String unable to Assign.", e);
}
textView = findViewById(R.id.answer);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
caluclate(num1, num2);
if(num2 == 0 && checkedId == R.id.div){
textView.setText("N.A.");
}else{
textView.setText(String.valueOf(ans));
}
}
});
}
float addition(float n1, float n2){
return n1 + n2;
}
float subtraction(float n1, float n2){
return n1 - n2;
}
float multiplication(float n1, float n2){
return n1 * n2;
}
float division(float n1, float n2){
return n1/n2;
}
void caluclate(float n1, float n2){
switch(radioGroup.getCheckedRadioButtonId()){
case R.id.add:
ans = addition (n1, n2);
break;
case R.id.sub:
ans = subtraction(n1, n2);
break;
case R.id.mul:
ans = multiplication(n1, n2);
break;
case R.id.div:
ans = division(n1, n2);
break;
}
}
}
XML 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DFDFDF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="First Number"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/first_number"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:hint="Enter Here"
android:inputType="numberDecimal|numberSigned"
android:padding="8dp"
android:background="@color/white"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Second Number"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<EditText
android:id="@+id/second_number"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:hint="Enter Here"
android:padding="8dp"
android:inputType="numberDecimal|numberSigned"
android:background="@color/white"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Choose Operation"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/radio_group"
android:paddingLeft="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Addition" />
<RadioButton
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Subtraction" />
<RadioButton
android:id="@+id/mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Multiplication" />
<RadioButton
android:id="@+id/div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:text="Division" />
</RadioGroup>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:text="Answer"
android:textColor="#2196F3"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:textSize="20dp"
android:padding="8dp"
android:inputType="number"
android:background="@color/white"
android:textStyle="bold" />
</LinearLayout>
我的应用程序崩溃了,直到我注释掉将 String 分配给 float 的那一行 然后我尝试使用 try-catch 块,但这只是阻止了我的应用程序崩溃
【问题讨论】:
-
您在
onCreate()方法中阅读了文本。这意味着您的EditText控件可能仍然是空的(用户还没有时间输入任何内容)并且Float.parseFloat()失败,因为空字符串的数值应该是多少?要修复它,您必须将EditText内容的读取和解析移动到onCheckedChanged()处理程序中。 -
您在代码中记录了错误,但没有将其添加到您的问题中。如果您再次遇到问题,建议您包含错误消息。
-
感谢@ThomasKläger 解释这个问题
-
当有人在
onCreate()函数中将音频分配给媒体播放器时会发生同样的情况
标签: java android android-studio crash android-edittext