簡體   English   中英

當我按下一個按鈕並且EditTexts為空時,它崩潰了

[英]When I press a button and the EditTexts are empty it crashes

我正在制作一個簡單的計算器,這是我第一個由我的應用程序制作的應用程序,並且我有兩個EditTexts ,如果我按4個計算按鈕中的任何一個應用程序崩潰,則我將設置一條Toast消息,以顯示EditText1和EditText2是否為空,否則顯示根據按下的按鈕進行計算...如何防止其崩潰? 這是我的MainActivity.java

public class MainActivity extends Activity {
    EditText number1text;
    EditText number2text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        number1text = (EditText) findViewById(R.id.num1text);
        number2text = (EditText) findViewById(R.id.num2text);
    }

    public void calcadd(View v) { // here
        if (number1text.getText().toString().isEmpty() || number2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
            "You Should Put Numbers To Do Calculation",
            Toast.LENGTH_LONG);
            msg.show();
        }
        else{

        }
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text + num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcminus(View v) { // here 
        if (number1text.getText().toString().isEmpty() || number2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
            "You Should Put Numbers To Do Calculation",
            Toast.LENGTH_LONG);
            msg.show();
        }
        else{

        }
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text - num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcdivide(View v) { // here
        if (number1text.getText().toString().isEmpty() || number2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
            "You Should Put Numbers To Do Calculation",
            Toast.LENGTH_LONG);
            msg.show();
        }
        else{

        }
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text / num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
    }

    public void calcmultiply(View v) { // here
        if (number1text.getText().toString().isEmpty() || number2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
            "You Should Put Numbers To Do Calculation",
            Toast.LENGTH_LONG);
            msg.show();
        }
        else{

        }
        Integer num1text = Integer.parseInt(number1text.getText().toString());
        Integer num2text = Integer.parseInt(number2text.getText().toString());
        Integer ans = num1text * num2text;

        TextView answer = (TextView) findViewById(R.id.ans);
        answer.setText("Answer:" + ans.toString());
     }  




    @Override
    public boolean onCreateOptionsMenu(Menu menu) { // here
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

這是我的activity_main.xml:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/num1"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        />

    <EditText
        android:id="@+id/num1text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/num1"
        android:layout_below="@+id/num1"
        android:ems="10"
        android:inputType="number" 
        >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/num1text"
        android:layout_below="@+id/num1text"
        android:text="@string/num2"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        />

    <EditText
        android:id="@+id/num2text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/num2"
        android:layout_below="@+id/num2"
        android:ems="10"
        android:inputType="number" 
        />

    <TextView
        android:id="@+id/ans"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/num2text"
        android:layout_below="@+id/num2text"
        android:layout_marginTop="52dp"
        android:text="@string/anstxt"
        android:textAppearance="?android:attr/textAppearanceMedium" 
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/num2text"
        android:layout_below="@+id/num2text"
        android:text="@string/btnadd" 
        android:onClick="calcadd"/>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/btnminus" 
        android:onClick="calcminus"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button01"
        android:layout_alignBottom="@+id/Button01"
        android:layout_toRightOf="@+id/Button01"
        android:text="@string/btnmultiply" 
        android:onClick="calcmultiply"/>

    <Button
        android:id="@+id/Button02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toRightOf="@+id/button2"
        android:text="@string/btndivide" 
        android:onClick="calcdivide"/>

</RelativeLayout>
else{

    }
    Integer num1text = Integer.parseInt(number1text.getText().toString());
    Integer num2text = Integer.parseInt(number2text.getText().toString());
    Integer ans = num1text + num2text;

    TextView answer = (TextView) findViewById(R.id.ans);
    answer.setText("Answer:" + ans.toString());

您在else {}語句之外進行工作,正在檢查它們是否為空,這是件好事,但是,如果它們為空“”,您仍然嘗試通過在else括號之外進行計算來進行計算。 您應該更改為我在下面顯示的內容...

else{
       Integer num1text = Integer.parseInt(number1text.getText().toString());
       Integer num2text = Integer.parseInt(number2text.getText().toString());
       Integer ans = num1text + num2text;

       TextView answer = (TextView) findViewById(R.id.ans);
       answer.setText("Answer:" + ans.toString());

      }

即使這不是您當前的問題

  1. 還要確保檢查除法函數中的除數是否為0:
    Integer ans = num1text / num2text;
  2. 您可以簡化代碼,以創建一個函數,該函數執行當前在每個函數中重復的操作(DRY;請勿重復自己)

@ 1:例如通過在除法函數中添加另一個if / else:

    public void calcdivide(View v) { // here
        if (number1text.getText().toString().isEmpty() || number2text.getText().toString().isEmpty()) {
            Toast msg = Toast.makeText(getBaseContext(),
            "You Should Put Numbers To Do Calculation",
            Toast.LENGTH_LONG);
            msg.show();
        }
        else{
            Integer num1text = Integer.parseInt(number1text.getText().toString());
            Integer num2text = Integer.parseInt(number2text.getText().toString());
            if (num2text>0)
            {
             //only do the division if divisor > 0
             Integer ans = num1text / num2text;
             TextView answer = (TextView) findViewById(R.id.ans);
             answer.setText("Answer:" + ans.toString());
            }
            else
            {
             //e.g. show a toastmessage here
            }
        }
    }

在要測試EditText是否為空的所有四個if結構中,請更改以下內容:

else{

}
Integer num1text = Integer.parseInt(number1text.getText().toString());
Integer num2text = Integer.parseInt(number2text.getText().toString());
Integer ans = num1text + num2text; // +, -, * or / depending

TextView answer = (TextView) findViewById(R.id.ans);
answer.setText("Answer:" + ans.toString());

為此(代碼需要 else塊中):

else{
    Integer num1text = Integer.parseInt(number1text.getText().toString());
    Integer num2text = Integer.parseInt(number2text.getText().toString());
    Integer ans = num1text + num2text;

    TextView answer = (TextView) findViewById(R.id.ans);
    answer.setText("Answer:" + ans.toString());
}

同樣,在每個getBaseContext() ,將getBaseContext()更改為getApplicationContext()

暫無
暫無

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

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