单击带有空 editText 的按钮时应用程序崩溃

发布于 2024-11-28 07:46:22 字数 842 浏览 2 评论 0原文

btnNadoplata.setOnClickListener(new View.OnClickListener()

        {
            public void onClick(View v) 
            {   
                long inputValue1 = Long.parseLong(text1.getText().toString());
                String encodedHash = Uri.encode("#");

                if (text1.getText().length() == 14 ) {

                    startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
                }else {

                    Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();

                    }
            }
        });

我有一个 editText,用户需要在其中输入一个 14 位数字。如果数字小于或大于 14 位,当用户单击按钮时,他会收到一条消息,提示输入不好。问题是当 editText 为空并且用户单击按钮时,应用程序崩溃。我怎样才能改变这个,所以如果editText为空,用户从上面的代码部分获取消息?

抱歉我的英语不好。

btnNadoplata.setOnClickListener(new View.OnClickListener()

        {
            public void onClick(View v) 
            {   
                long inputValue1 = Long.parseLong(text1.getText().toString());
                String encodedHash = Uri.encode("#");

                if (text1.getText().length() == 14 ) {

                    startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
                }else {

                    Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();

                    }
            }
        });

I have one editText, in wich user needs to input a number14 digit number. If number is less or more than an 14 digits, when user clikc on button, he gets the message that say input is not good. The problem is when editText is empty, and user click on button, app crashes. how can i change this, so if editText is empty, user gets message from above code part ??

Sory for my bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谷夏 2024-12-05 07:46:22

它可能会在这一行崩溃:

long inputValue1 = Long.parseLong(text1.getText().toString());

事实上,如果您的 EditText text1 中有一个空字符串,则函数 parseLong() 将抛出 NumberFormatException > 例外。

在继续之前,您应该测试 text1 的文本值:

public void onClick(View v) 
{   
    if (text1.getText().toString().compareTo("") == 0)
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...

或者您可以添加 try/catch 指令来捕获 Long.parseLong()< 抛出的异常/代码>。

public void onClick(View v) 
{   
    try
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...
    }
    catch (NumberFormatException nfe)
    {
        ...
    }

It might crash on this line:

long inputValue1 = Long.parseLong(text1.getText().toString());

In fact, if you have an empty string in your EditText text1, the function parseLong() will throw a NumberFormatException exception.

You should test the value of the text of text1 before continuing:

public void onClick(View v) 
{   
    if (text1.getText().toString().compareTo("") == 0)
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...

Or you can add try/catch instruction to catch the exception thrown by Long.parseLong().

public void onClick(View v) 
{   
    try
    {
        long inputValue1 = Long.parseLong(text1.getText().toString());
        String encodedHash = Uri.encode("#");
        ...
    }
    catch (NumberFormatException nfe)
    {
        ...
    }
舂唻埖巳落 2024-12-05 07:46:22

您应该在解析之前测试您的输入长度。解析崩溃。

 public void onClick(View v) 
 { 
     if( text1.getText().length() <14 )
     {
        Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();
        return;
     }//if

     long inputValue1 = Long.parseLong(text1.getText().toString());
     String encodedHash = Uri.encode("#");
     startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
 }//met

另一种方法可能是用 try/catch/block 包围解析,但它的效率比这个简单的测试低,但如果用户输入非数字,则更稳健。

问候,
史蒂芬

You should test your input length before parsing. Parsing crashes.

 public void onClick(View v) 
 { 
     if( text1.getText().length() <14 )
     {
        Toast.makeText(bonbon.this, "Krivi kod. Unesi 14-znamenkasti kod sa bona!", Toast.LENGTH_SHORT).show();
        return;
     }//if

     long inputValue1 = Long.parseLong(text1.getText().toString());
     String encodedHash = Uri.encode("#");
     startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:*123*" + inputValue1 + encodedHash)));
 }//met

An alternative could be to surround parsing with a try/catch/block, but it's less efficient than this simple test, but more robust if user types non digits.

Regards,
Stéphane

临走之时 2024-12-05 07:46:22
objText = (EditText)findViewById(R.id.txtPurchasePrice);
String strPurchasePrice = objText.getText().toString();   
objText = (EditText)findViewById(R.id.txtSalePrice);
String strSalePrice = objText.getText().toString();
objText = (EditText)findViewById(R.id.txtShares);
String strShares = objText.getText().toString();

if(strPurchasePrice.trim().equals("") && strSalePrice.trim().equals("") && strShares.trim().equals("")) {      
   Toast.makeText(getApplicationContext(), "Please insert the Values..", 10000).show();
}
objText = (EditText)findViewById(R.id.txtPurchasePrice);
String strPurchasePrice = objText.getText().toString();   
objText = (EditText)findViewById(R.id.txtSalePrice);
String strSalePrice = objText.getText().toString();
objText = (EditText)findViewById(R.id.txtShares);
String strShares = objText.getText().toString();

if(strPurchasePrice.trim().equals("") && strSalePrice.trim().equals("") && strShares.trim().equals("")) {      
   Toast.makeText(getApplicationContext(), "Please insert the Values..", 10000).show();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文