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

时间:2013-09-14 17:34:32

标签: java android

代码中缺少什么?

我这里有问题。我这里有2个edittext。一个用于金额,一个用于密码。我的应用程序就像一个魅力,除非我在edittexts上输入NONE,IT CRASHES(不幸的是停止)。我错过了活动的任何代码吗?请帮忙。这是我的代码。

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();
                }
            }
        });

    }

这是我的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)

7 个答案:

答案 0 :(得分:6)

这是NumberFormatExpection

上未被捕捉的Integer.parseInt(txtbox1.getText().toString()) 调用此方法时,

txtbox1为空,因此您调用的Integer.parseInt("")会引发NumberFormatExpection

答案 1 :(得分:3)

您正在输入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();
}

投掷错误

您必须仅使用数字 EditText 设置属性inputType  像这样

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

答案 2 :(得分:1)

在将Amount转换为 int 之前检查输入是否为空,否则会出现 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();


    }
    }
    });

答案 3 :(得分:1)

问题是如果EditText没有输入任何内容,那么将其解析为Integer会给你一个错误(因为没有要解析的数字)。

EditText获取输入并将其解析为Integer时,只需添加if结构,如下所示:

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

答案 4 :(得分:0)

你试图通过尝试解析txtBox1..getText().toString()并将其重新发送回错误吐司而尝试捕获可能出现的错误?

答案 5 :(得分:0)

嘿,你要做一个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();
            }
        }
    });

}
希望它有所帮助...

答案 6 :(得分:-1)

我找到了解决方案。 我设置android:text="0" 我的问题消失了。 :))