当进入Android应用程序崩溃,并且没有任何按键pressed [英] Android app crashes when nothing is entered and button is pressed

查看:153
本文介绍了当进入Android应用程序崩溃,并且没有任何按键pressed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,想寻求一些帮助:
用户输入一个EDITTEXT和presses一个数字/点击此按钮,获得在回答现场文本显示的转换。

Hi I am working on an Android App and would like to ask for some help: The user enters a numbers on a editText and presses/clicks the button to get a conversion displayed in answer field text.

不幸的是,如果没有任何输入,用户点击按钮的应用程序崩溃。

Unfortunately if nothing is entered and the user clicks the button the application crashes.

谁能帮我把它变成code:

Could anyone help me put it into code:

如果EDITTEXT为空,单击按钮显示0.00答案文本字段。

If editText is empty and button is clicked display 0.00 in the answer text field.

或其他方式,如果EDITTEXT是空的,按钮pressed显示敬酒消息

or the other way if editText is empty and button is pressed display toast message

如果任何人都明白我在说什么,可以帮助我将非常AP preciate它,因为我想在发布之前提高我对用户的应用程序; - )

If anyone understands what I am talking about and can help I would greatly appreciate it as I want to improve my applications for users before publishing ;-)

我更新code:

    EditText editText = (EditText)findViewById(R.id.editText);
Double num1 = 0.0;
final String myStr = editText.getText().toString();
if (!myStrisEmpty())
{
    num1 = Double.parseDouble(myStr);
}
else
{
    Toast.makeText(getApplicationContext(), getResources().getString(R.string.noinput),
        Toast.LENGTH_LONG).show();
}

问题是这样的:
 。最终的字符串myStr的= editText.getText()的toString(); // EDITTEXT是红色的
    如果(!myStrisEmpty())//(!myStrisEmpty())是在红色

Problem is this: final String myStr = editText.getText().toString(); //editText is in red if (!myStrisEmpty()) //(!myStrisEmpty()) is in red

我一直试图几个小时,几天acomplish此任务。简单地取得用户在其中输入然后一个数字点击按钮来转换的数量和下方显示它变成一个TextView回答一个应用程序。如果我不输入任何内容,然后单击按钮的应用程序崩溃。我想敬酒消息,而不是它崩溃说请输入一个数字

Ive been trying for hours and days to acomplish this task. simply have made a app where the user enters a number then clicks the button to convert the number and display it below into a textview answer. If i dont enter anything and click the button the app crashes. I would like a toast message instead of it crashing saying "Please Enter a Number"

推荐答案

您正试图为空字符串转换为数字。结果
你应该做的是检查,如果该字符串不是空的:

You are trying to convert an empty string to a number.
What you should do is a check if the string is not empty:

更改这些行:

EditText editText = (EditText) findViewById(R.id.editText);
Double num1 = Double.parseDouble(editText.getText().toString());

EditText editText = (EditText)findViewById(R.id.editText);
Double num1 = 0.0;
final String myStr = editText.getText().toString();
if (!myStr.isEmpty())
{
    num1 = Double.parseDouble(myStr);
}
else
{
    Toast.makeText(getApplicationContext(), getResources().getString(R.string.noinput),
        Toast.LENGTH_LONG).show();
}

您可以用硬$ C $替换getResources()。的getString(R.string.noinput)(设置在 /values​​/strings.xml 文件夹) CD字符串(但是这不是一个好的做法),像你没有输入任何值

You might replace getResources().getString(R.string.noinput) (set it in your /values/strings.xml folder) by a hardcoded string (but that's not a good practice), like "You didn't enter any value"

和这样的:

answer.setText(ans.toString());

应该是:

answer.setText("" + ans);

这篇关于当进入Android应用程序崩溃,并且没有任何按键pressed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆