简体   繁体   English

当我在edittext上输入任何内容时,应用程序崩溃

[英]App crashes when I entered nothing on edittext

What's missing in code? 代码中缺少什么?

I have a problem here. 我这里有问题。 I have here 2 edittext. 我这里有2个edittext。 One for amount and one for password. 一个用于金额,一个用于密码。 My app works like a charm except when I entered NONE on the edittexts, IT CRASHES (Unfortunately stop). 我的应用程序就像一个魅力,除非我在edittexts上输入NONE,IT CRASHES(不幸的是停止)。 Am I missing any code on my activity? 我错过了活动的任何代码吗? Please help. 请帮忙。 Here's my code. 这是我的代码。

public class MainActivity extends Activity {
    Button REDIRECT;
    private EditText txtbox1,txtbox2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        REDIRECT = (Button) findViewById(R.id.button1);
        txtbox1= (EditText) findViewById(R.id.editText1);  
        txtbox2= (EditText) findViewById(R.id.editText2);

        REDIRECT.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int Amount = Integer.parseInt(txtbox1.getText().toString());
                String Password = txtbox2.getText().toString();


                if(Amount<=50&&Amount>=1 & Password.equals("TUBOL"))
                {

                final Intent i = new Intent(MainActivity.this, Redirect.class);               
                startActivity(i);
                }
                else 
                {
                    Toast.makeText(MainActivity.this, "INVALID", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

Here is my logcat: 这是我的logcat:

09-15 01:46:34.641: E/AndroidRuntime(30540): FATAL EXCEPTION: main
09-15 01:46:34.641: E/AndroidRuntime(30540): java.lang.NumberFormatException: Invalid int: ""
09-15 01:46:34.641: E/AndroidRuntime(30540):    at java.lang.Integer.invalidInt(Integer.java:138)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at java.lang.Integer.parseInt(Integer.java:359)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at java.lang.Integer.parseInt(Integer.java:332)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at com.example.pwordlockdown.MainActivity$1.onClick(MainActivity.java:29)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.view.View.performClick(View.java:3549)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.view.View$PerformClick.run(View.java:14400)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.os.Handler.handleCallback(Handler.java:605)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.os.Looper.loop(Looper.java:154)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at android.app.ActivityThread.main(ActivityThread.java:4945)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at java.lang.reflect.Method.invoke(Method.java:511)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-15 01:46:34.641: E/AndroidRuntime(30540):    at dalvik.system.NativeStart.main(Native Method)

It's an uncaught NumberFormatExpection on Integer.parseInt(txtbox1.getText().toString()) 它是Integer.parseInt(txtbox1.getText().toString())上未被捕获的NumberFormatExpection Integer.parseInt(txtbox1.getText().toString())

txtbox1 is empty when this method is called, so you're calling Integer.parseInt("") which throws a NumberFormatExpection . txtbox1此方法时, txtbox1为空,因此您调用的是Integer.parseInt("") ,它会抛出NumberFormatExpection

You are entering String and trying to parse it to Int 您正在输入String并尝试将其解析为Int

int Amount = 0;

try
{
    Amount = Integer.parseInt(txtbox1.getText().toString());
}
catch(NumberFormatException e)
{
    // Sep 14, 2013 11:26:26 PM
    Log.e("Exception","DownloadFileTask.onPostExecute.NumberFormatException"+String.valueOf(e.getMessage()));
    e.printStackTrace();
}

This throwing error 这个投掷错误

You must use for number only EditText set property inputType like this 您必须使用仅限编号的EditText设置属性inputType

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" >
</EditText>

Check if the input is not null before converting Amount it to int otherwise there will be NumberFormatException 在将Amount转换为int之前检查输入是否为null,否则将出现NumberFormatException

 REDIRECT.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        String a=txtbox1.getText().toString();
        if(a.equals(" "))
        {

        Toast a1 =Toast.makeText(getApplicationContext(), "Enter the Amount", Toast.LENGTH_LONG);
        a1.show();

        }
        else 
    {

    int Amount = Integer.parseInt(txtbox1.getText().toString());
            String Password = txtbox2.getText().toString();


            if(Amount<=50&&Amount>=1 & Password.equals("TUBOL"))
            {

            final Intent i = new Intent(MainActivity.this, Redirect.class);               
            startActivity(i);
            }
            else 
            {
                Toast.makeText(MainActivity.this, "INVALID", Toast.LENGTH_LONG).show();


    }
    }
    });

The problem is that if nothing is entered into the EditText , then parsing it to an Integer would give you an error (because there are no numbers to parse). 问题是如果没有输入EditText ,那么将其解析为Integer会给你一个错误(因为没有要解析的数字)。

When taking input from your EditText and parsing it to an Integer , simply add an if structure, like so: EditText获取输入并将其解析为Integer ,只需添加if结构,如下所示:

if (!(editText.getText().toString().equals(""))) // if edit text is NOT blank
{
    int num = Integer.parseInt(editText.getText().toString());
}

你试图通过尝试解析txtBox1..getText().toString()并尝试将其重新路由到错误吐司来捕获可能出现的错误怎么样?

hey you gotta do an if statement to check if the textBox is empty or not... 嘿你要做一个if语句检查textBox是否为空...

public class MainActivity extends Activity {
Button REDIRECT;
private EditText txtbox1,txtbox2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    REDIRECT = (Button) findViewById(R.id.button1);
    txtbox1= (EditText) findViewById(R.id.editText1);  
    txtbox2= (EditText) findViewById(R.id.editText2);

    REDIRECT.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        if(!txtbox1.getText().toString().equals("")){

            int Amount = Integer.parseInt(txtbox1.getText().toString());
            String Password = txtbox2.getText().toString();


            if(Amount<=50&&Amount>=1 & Password.equals("TUBOL"))
            {

            final Intent i = new Intent(MainActivity.this, Redirect.class);               
            startActivity(i);
            }
            else 
            {
                Toast.makeText(MainActivity.this, "INVALID", Toast.LENGTH_LONG).show();
            }
            }else{
                Toast.makeText(MainActivity.this, "Amount Box empty...", Toast.LENGTH_LONG).show();
            }
        }
    });

}

hope it helps... 希望能帮助到你...

I find my solution. 我找到了解决方案。 I set android:text="0" and my problem is gone. 我设置android:text="0" ,我的问题就消失了。 :)) :))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下按钮时未输入任何内容时,Android应用程序崩溃 - Android app crashes when nothing is entered when button is pressed 未输入任何内容并按下按钮时,Android应用程序崩溃 - Android app crashes when nothing is entered and button is pressed 当 EditText 输入为空时,Android 应用程序崩溃 - Android Application Crashes When EditText Is Entered Empty 当我尝试添加监听器时,EditText会崩溃应用程序 - EditText crashes app when I try to add listener 当EditText为空并且按下按钮时,我的应用程序崩溃 - My app crashes when an EditText is empty and I press a button 当我点击保存按钮时没有输入任何内容时应用程序崩溃。 应该弹出错误 - App crashes when nothing is inputted when I click save button. Error should pop up 为什么我的应用程序在我将字符串(来自 EditText)解析为 Float 时崩溃 - Why my app crashes when I parse string (from EditText) to Float 当 EditText 为空时,Java 简单数字游戏应用程序崩溃 - Java Simple number game App crashes when EditText is empty 尝试从EditText解析整数时,应用程序崩溃 - App crashes when attempting to parse integer from EditText 使用editText.setSelection(position)定义光标位置时,应用崩溃。 - App crashes when defining cursor position with editText.setSelection(position);
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM