3

代码中缺少什么?

我这里有问题。我这里有 2 个编辑文本。一个用于金额,一个用于密码。我的应用程序就像一个魅力,除了当我在编辑文本上输入 NONE 时,它崩溃了(不幸的是停止)。我是否遗漏了我的活动中的任何代码?请帮忙。这是我的代码。

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

    }

这是我的日志:

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)
4

7 回答 7

6

这是一个未被抓住NumberFormatExpectionInteger.parseInt(txtbox1.getText().toString())

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

于 2013-09-14T17:41:58.973 回答
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>
于 2013-09-14T17:41:35.530 回答
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();


    }
    }
    });
于 2013-09-14T18:00:02.800 回答
1

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

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

if (!(editText.getText().toString().equals(""))) // if edit text is NOT blank
{
    int num = Integer.parseInt(editText.getText().toString());
}
于 2013-09-14T18:03:09.587 回答
0

您如何尝试捕获尝试解析时抛出的可能错误,txtBox1..getText().toString()然后将其路由回错误吐司?

于 2013-09-14T17:49:06.917 回答
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();
            }
        }
    });

}

希望能帮助到你...

于 2013-09-14T17:50:45.523 回答
-1

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

于 2013-09-14T17:55:28.693 回答