【问题标题】:When I press a button and the EditTexts are empty it crashes当我按下一个按钮并且 EditTexts 为空时它崩溃
【发布时间】:2017-04-13 22:11:51
【问题描述】:

我正在制作一个简单的计算器,这是我的第一个应用程序,我有两个 EditTexts,如果我按下 4 个计算按钮中的任何一个,应用程序就会崩溃,如果 EditText1 和 EditText2 是,我已经设置了一个 toast 消息来显示为空,如果不按按钮进行计算...如何防止它崩溃? 这是我的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>

【问题讨论】:

  • 您正在对空字符串调用 Integer.parseInt。它崩溃了。不要那样做。
  • 发布堆栈跟踪。总是。
  • 我在没有 stracktrace 的手机上运行它
  • @Odie 你的 else 语句都是空的。移动您的代码以在那些 else 语句中进行计算。
  • 我该如何解决这个问题@nzk2 请在下面回答

标签: java android


【解决方案1】:
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());

      }

【讨论】:

  • 等一下,我将在我的模拟器上运行它,看看发生了什么,因为如果没有堆栈跟踪,我很难知道你的问题
  • 你确定你改变了所有四种方法的else吗?您需要更改 calcadd calcminus calcdivide 和 calcmultiply 因为它们都有 else 之外的内容
  • 我都改了
  • 好的,你将不得不给我一秒钟我正在安装新的 SDK
  • @Odie 尝试将所有 Toast 中的 getBaseContext() 更改为 getApplicationContext()
【解决方案2】:

即使这不是你当前的问题

  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
            }
        }
    }

【讨论】:

  • 见上文。我改进了我的答案
【解决方案3】:

在您测试 EditTexts 是否为空的所有四个 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());
}

另外,在您的每个 Toast 中,将 getBaseContext() 更改为 getApplicationContext()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-21
    • 2021-12-22
    相关资源
    最近更新 更多