当EditText为空并且我按下按钮时,我的应用程序崩溃



为什么当我点击按钮时,EditText中没有任何内容,程序会崩溃?

该应用程序用于计算负载安全性。用户输入值​​并得到他们需要的带子数量。但当一个字段空闲时,应用程序会崩溃

代码:

public class Ladungssicherung extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

EditText stfinput;
EditText gewichtinput;
EditText winkelinput;
String text;
double k = 1.5;
int cZ = 1; // beschleunigungsbeiwert nach unten
double cX = 0.8; // beschleunigungsbeiwert nach vorne

Dialog epicDialog;
TextView unicode, ergebnissFeld;
ImageView muinfoButton, closemuButton, infoalphaButton;
@SuppressLint("StringFormatInvalid")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_ladungssicherung);
winkelinput = (EditText) findViewById(R.id.winkelInput);
ergebnissFeld = (TextView) findViewById(R.id.ergebniss);
gewichtinput = (EditText) findViewById(R.id.gewichtInput);
stfinput = (EditText) findViewById(R.id.stfInput);
SharedPreferences sharedPreferences = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
text = sharedPreferences.getString("text", "");
winkelinput.setText(text);
sharedPreferences.edit().remove("text").commit();
epicDialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen);

muinfoButton = (ImageView) findViewById(R.id.infomuIcon);
closemuButton = (ImageView) findViewById(R.id.closemuinfo);
infoalphaButton = (ImageView) findViewById(R.id.infoalpha);
muinfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showmuinfo();

}
});
infoalphaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showalphawinkel();
}
});

//Spinner code
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

}
public void showalphawinkel() {
Intent intent = new Intent(this, WinkelmessActivity.class);
startActivity(intent);

}

public void showmuinfo() {
epicDialog.setContentView(R.layout.muinfo);
closemuButton = (ImageView) epicDialog.findViewById(R.id.closemuinfo);
closemuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
epicDialog.show();
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void ergebnissFromel(View v) {
Spinner feld4 = (Spinner) findViewById(R.id.spinner1);
Integer zahl1 = Integer.parseInt(gewichtinput.getText().toString());
Integer zahl2 = Integer.parseInt(winkelinput.getText().toString());
Integer zahl3 = Integer.parseInt(stfinput.getText().toString());
String spinner = feld4.getSelectedItem().toString();

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".Ladungssicherung">

<ImageView
android:id="@+id/infoalpha"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/winkelInput"
android:layout_alignStart="@+id/infomuIcon"
app:srcCompat="@drawable/info_icon" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="97dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="223dp"
android:layout_toEndOf="@+id/textView">
</Spinner>

<TextView
android:id="@+id/textView"
android:layout_width="117dp"
android:layout_height="29dp"
android:layout_above="@+id/gewichtInput"
android:layout_alignStart="@+id/gewichtInput"
android:layout_marginBottom="-108dp"
android:text="Gewicht"
android:textSize="20sp"
android:textStyle="bold|italic" />
<EditText
android:id="@+id/gewichtInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108dp"
android:ems="10"
android:hint="kg"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/winkelInput"
android:layout_marginBottom="-211dp"
android:layout_toStartOf="@+id/spinner1"
android:text="Winkel Alpha"
android:textSize="20sp"
android:textStyle="bold|italic" />

<EditText
android:id="@+id/winkelInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="211dp"
android:ems="10"
android:hint="@string/alpha"
android:inputType="textPersonName" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/stfInput"
android:layout_alignStart="@+id/textView"
android:text="Vorspannkraft je Gurt"
android:textSize="20sp"
android:textStyle="bold|italic" />

<EditText
android:id="@+id/stfInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView5"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="daN"
android:inputType="textPersonName" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="37dp"
android:layout_height="29dp"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/textView5"
android:layout_marginTop="116dp"
app:srcCompat="@drawable/gewicht" />

<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="175dp"
android:background="@color/grau">
</View>
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="277dp"
android:background="@color/grau">
</View>
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="275dp"
android:background="@color/grau">
</View>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignTop="@+id/spinner1"
android:layout_marginStart="46dp"
android:text="@string/mue"
android:textSize="25sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="15dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="-3dp"
android:layout_toStartOf="@+id/textView6"
android:text="S"
android:textSize="23sp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/stfInput"
android:layout_marginStart="3dp"
android:layout_alignStart="@+id/textView4"
android:text="TF"
android:textSize="23sp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/winkelInput"
android:layout_alignStart="@+id/textView4"
android:text="@string/alpha"
android:textSize="23sp" />
<ImageView
android:id="@+id/infomuIcon"
android:layout_width="38dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/spinner1"
android:layout_marginTop="-2dp"
android:layout_marginEnd="17dp"
app:srcCompat="@drawable/info_icon" />
<TextView
android:id="@+id/titelNiederzurren"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:textSize="20sp"
android:textColor="@color/blue"
android:text="Niederzurren" />

<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="199dp"
android:background="@color/grau">
</View>
<TextView
android:id="@+id/ergebniss"
android:layout_width="154dp"
android:layout_height="36dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="53dp"
android:background="@color/greensmiley"
android:text="" />
<Button
android:id="@+id/buttonladungssicherung"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:text="Button"
android:onClick="ergebnissFromel"
/>

</RelativeLayout>

您可能会得到NumberFormatException,因为您试图将空字符串解析为Integer。用try-catch块环绕Integer.parseInt(gewichtinput.getText().toString())行,应该没问题。

试试这些代码,让我们用您看到的更新:

public void ergebnissFromel(View v) {
Spinner feld4 = (Spinner) findViewById(R.id.spinner1);
Integer zahl1 = 0;
Integer zahl2 = 0;
Integer zahl3 = 0;
try
{
zahl1 = Integer.parseInt(gewichtinput.getText().toString());
zahl2 = Integer.parseInt(winkelinput.getText().toString());
zahl3 = Integer.parseInt(stfinput.getText().toString());
} catch (Exception e) {
Toast.makeText(this,e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
String spinner = feld4.getSelectedItem().toString();

if ( zahl1.equals("") || zahl1 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
if ( zahl2.equals("") || zahl2 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
if ( zahl3.equals("") || zahl3 == null){
Toast.makeText(this, "Bitte geben Sie das Gewicht an", Toast.LENGTH_SHORT).show();
}
else {
// Grad wird in sinus alpha umgerechnet
double newsinus = Math.sin(Math.toRadians(Float.valueOf(zahl2)));
double wert1 = cX - Float.valueOf(spinner) * cZ;
double wert2 = Float.valueOf(spinner) * newsinus;
// Formel Niederzurren Algorithmus
double wert3 = wert1 * zahl1;
double wert4 = wert2 * k;
double wert5 = wert3 / wert4;
double wert6 = Math.round(wert5);
double wert7 = wert6 / zahl3;
double wert8 = Math.ceil(wert7);
ergebnissFeld.setText(String.valueOf(Math.round(wert8)));
}
}

通常,当你的应用程序崩溃时,你会发现记录的堆栈跟踪将帮助你了解问题所在。你可以在Android Studio的"调试|控制台"选项卡中找到日志。或者,你可以从终端/shell窗口使用adb命令。我喜欢使用"adb-logcat-v时间"。

在解析为integer之前,您应该检查输入是否为null。此外,您正在zahl1上调用一个方法,然后检查它是否为null。

String strZahl1 = gewichtinput.getText().toString(); //Get input as String
if(strZahl1 == null || strZahl1.isEmpty()) //First make sure that strZahl1 is not null, then check wether it is empty
{
Toast.makeText(this, "Bitte geben Sie das Gewicht an",Toast.LENGTH_SHORT).show();
}
Integer zahl1 = Integer.parseInt(strZahl1);

如果用户能够在文本字段中输入数字以外的内容,则可以在解析过程中使用一个try块:

Integer zahl1;
try
{
zahl1 = Integer.parseInt(strZahl1);
}
catch(NumberFormatException e)
{
Toast.makeText(this, strZahl1 + " ist keine gültige Zahl", Toast.LENGTH_SHORT).show();
}

您的应用程序崩溃,因为.getText((.toString是一个空字符串。Java无法将空字符串转换为整数。

一个更好的方法是做一些类似的事情

Integer zahl1 ; 
if((gewichtinput.getText() != null) && (!gewichtinput.getText().toString.isEmpty()))
{
zahl1 = Integer.parseInt(gewichtinput.getText().toString());
}
else
{
zahl1 = 0 ; 
}

对所有其他编辑文本执行相同操作。它应该有望在中工作

最新更新