簡體   English   中英

當EditText為空並且按下按鈕時,我的應用程序崩潰

[英]My app crashes when an EditText is empty and I press a button

為什么當我單擊按鈕並且在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,因為您正在嘗試將空String解析為Integer try catch塊將Integer.parseInt(gewichtinput.getText().toString())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的Debug | Console標簽中找到日志。 或者,您可以從終端/外殼窗口使用adb命令。 我喜歡使用“ adb logcat -v時間”。

在解析為整數之前,您應該檢查輸入是否為空。 此外,您正在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 ; 
}

對所有其他編輯文本執行相同的操作。 它有望工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM