【问题标题】:validate the edittext field inside alertdialog验证 alertdialog 中的 edittext 字段
【发布时间】:2011-09-22 22:07:42
【问题描述】:

我正在使用这个函数来插入数据库。我想验证来自两个编辑文本字段的输入。每当我在不提供任何输入的情况下按下确定按钮时,程序就会崩溃。我也尝试打印这些值,但它没有显示在 logcat 中。我做错了什么吗?

    private void add() {
    LayoutInflater inflater=LayoutInflater.from(this);
    final View addView=inflater.inflate(R.layout.add_country, null);

    new AlertDialog.Builder(this)
        .setTitle("Add new country/year")
        .setView(addView)
        .setPositiveButton("OK", 
                new DialogInterface.OnClickListener() {
                                                public void onClickDialogInterface                                                       dialog,int whichButton) {
                        /* Read alert input */
                        EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
                        String country = editCountry.getText().toString();
                        EditText editYear =(EditText)addView.findViewById(R.id.editYear);
                        int year = Integer.parseInt( editYear.getText().toString() );
                        if(editCountry.getText().toString().trim().length()>0 && editYear.getText().toString().trim().length()>0){
                            /* Open DB and add new entry */
                            db.open(); 
                            db.insertEntry(country,year);

                            /* Create new cursor to update list. Must be possible to handle
                             * in a better way. */
                            entryCursor = db.fetchAllEntries();    // all country/year pairs                            
                            adapter = new SimpleCursorAdapter(CountryEditor.this,
                                    R.layout.country_row,entryCursor,
                                    new String[] {"country", "year"},
                                    new int[] {R.id.country, R.id.year});
                            setListAdapter(adapter);
                            db.close();
                        }
                        else{
                            Toast.makeText(CountryEditor.this,
                                    "You need to enter Country AND Year.", Toast.LENGTH_LONG).show();
                        }
                    }
                })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int whichButton) {
                        // ignore, just dismiss
                    }
                })
        .show();
}

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    您正在调用 editBlah.getText().toString() 可以返回 "";

    将其解析为整数时,将抛出异常。

    ( 也可能是,如果您在已初始化为 null 的视图上调用 .getText()(即,您错误地为所需的 ID 指定了 ID),则会抛出 NullPointerException。没有Stacktrace 你无法分辨出哪个 - 尝试在可能的情况下发布带有问题的堆栈跟踪)。

    你的问题是正确的 - 你需要做的是验证你得到的输入:即:

    int year = Integer.parseInt( editYear.getText().toString() );
    

    应该是:

    if(editYear.getText().toString().equalsIgnoreCase("")) {
        // Cannot parse into an int therefore perform some action which will notify the 
        // user they haven't entered the correct value.
    }
    

    如果您已经要验证您的 int 值,甚至可以使用以下方法:

    int year = Integer.parseInt( editYear.getText().toString().equalsIgnoreCase("") ? 
            "-1" : editYear.getText().toString());
    

    【讨论】:

    • 是的,事实上 int year = Integer.parseInt(editYear.getText().toString());由于 editYear.getText().toString() 返回“”,因此引发了异常。我只需要改组几行代码,即只需在此行之前加上“if”条件 int year = Integer.parseInt(editYear.getText().toString() );所以我只是测试它以确保输入有效,然后再将其解析为整数。 if(editCountry.getText().toString().trim().length()>0 && editYear.getText().toString().trim().length()>0){ int year = Integer.parseInt(编辑年.getText().toString()); } 感谢您的帮助
    【解决方案2】:

    editCountry.getText() 等于空字符串?空指针异常

    【讨论】:

      猜你喜欢
      • 2017-03-08
      • 1970-01-01
      • 2014-07-08
      • 2012-10-01
      • 2013-12-09
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      相关资源
      最近更新 更多