验证动态生成的EditTexts

时间:2014-08-27 11:21:28

标签: java android android-edittext

我有一个类,用户可以动态生成EditText视图。我的目标是在用户继续下一页时验证它们是否为空。如果是,则验证触发并停止移动到另一页的过程,直到用户填写他/她创建的EditText字段。

这就是我所拥有的:

EditText生成

int EditTextID = 5000;
insertPoint = findViewById(R.id.questionsContainer);

if(v.getId() == R.id.makeLayoutButton){

    //get form template which has an EditText of id "questionAnswerEt"
    v1 = vi.inflate(R.layout.form_template, null);

    //add it into my view
    ((LinearLayout) insertPoint).addView(v1);

    v1.setId(formParentID);

    //get edittext
    questionAnswer = (EditText)v1.findViewById(R.id.questionAnswerET);

    //set id
    questionAnswer.setId(EditTextID);

    EditTextID++;
}

验证

if(v.getId() == R.id.bNext){
    for(int x = 5000; x <= 5007; x++){
        questionAnswer = (EditText)findViewById(x);
        String questionAnswerValidator = (String) questionAnswer.getText().toString();

        if(questionAnswerValidator.length() == 0){
           ((EditText) questionAnswer.findViewById(x))
           .setError( "Question Title is Required!" );
        }
    }
}

循环从5000开始,因为生成的EditTexts已手动设置从5000开始的ID,然后在每次用户决定生成另一个EditText时递增。然后,我通过引用“questionAnswer”循环访问ID来获取EditText。然后我将questionAnswer(id为x)的字符串值放入String“questionAnswerValidator”中。然后检查它是否为空,如果是,它将在相应的EditTexts上产生一个空的错误消息。问题是,当验证开始给我一个空指针异常时,应用程序崩溃了。关于我哪里出错的任何线索?谢谢。

日志:

08-27 14:27:02.084: W/dalvikvm(16285): threadid=1: thread exiting with uncaught exception (group=0x4166fe18)
08-27 14:27:02.084: E/AndroidRuntime(16285): FATAL EXCEPTION: main
08-27 14:27:02.084: E/AndroidRuntime(16285): Process: com.example.testing, PID: 16285
08-27 14:27:02.084: E/AndroidRuntime(16285): java.lang.NullPointerException
08-27 14:27:02.084: E/AndroidRuntime(16285):    at com.example.testing.MakeQuestion.onClick(MakeQuestion.java:212)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.view.View.performClick(View.java:4480)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.view.View$PerformClick.run(View.java:18686)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.os.Handler.handleCallback(Handler.java:733)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.os.Looper.loop(Looper.java:157)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at android.app.ActivityThread.main(ActivityThread.java:5872)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at java.lang.reflect.Method.invoke(Method.java:515)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
08-27 14:27:02.084: E/AndroidRuntime(16285):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

尝试使用此代码进行验证TextUtil.isEmpty(questionAnswerValidator)检查是否为null和0长度

String questionAnswerValidator;
if(v.getId() == R.id.bNext){
for(int x = 5000; x <= 5007; x++){
    questionAnswer = (EditText)findViewById(x);
    questionAnswerValidator = questionAnswer.getText().toString();

    if(TextUtil.isEmpty(questionAnswerValidator)){
       questionAnswer.setError( "Question Title is Required!" );
    }
  }
}

答案 1 :(得分:0)

好像我在循环中走出界限。这就是诀窍:

for(int x = 5000; x < EditTextID; x++){
    questionAnswer = (EditText)findViewById(x);
    if(questionAnswer.getText().toString().length() == 0){  
        questionAnswer.setError( "Question Title is Required!" );
    }
}

唯一的问题是它现在在相应的空EditTexts字段上显示setError图标但是在我开始在相应的EditText中输入之前一直保留错误消息。怪异。

答案 2 :(得分:0)

正如Frisbetarian告诉你的那样,你可以使用他的答案并添加.requestFocus,以便错误出现在EditText中。请注意,显示上一个错误的EditText将收到错误的焦点,直到您特别填写EditText。

for(int x = 5000; x < EditTextID; x++){
    questionAnswer = (EditText)findViewById(x);
    if(questionAnswer.getText().toString().length() == 0){  
        questionAnswer.setError( "Question Title is Required!" );
        questionAnswer.requestFocus();

    }
}