将EditText引用为字符串

时间:2018-10-14 10:17:52

标签: java xml android-studio

我构建了一个包含一个方法的简单应用程序,该应用程序应检查用户插入的电话号码是否有效。 所以我做了以下步骤:

  1. 在Java文件中声明编辑文本
  2. 将该变量引用到xml文件中的变量
  3. 获取文本并输入该变量的字符串
  4. 写出引用“ phone”变量的方法

声明(第1步):

private static EditText phone_et_c

refare(步骤2):

phone_et_c=(EditText) findViewById(R.id.phone_et_c);

获取文本并输入字符串(第3步):

String phone=phone_et_c.getText().toString();

和Mathoud是(步骤4):

public static boolean isValid(String phone)
{
    if ((phone.length()==10)||(phone.charAt(0)==0&&phone.charAt(1)==5)){
        return true;
    }
    else
        return false;
}

我的错误是:

  

10-14 10:06:05.885 5140-5140 / com.miara.yagel.maspera E / Android运行时:致命异常:主要                                                                              流程:com.miara.yagel.maspera,PID:5140                                                                              java.lang.StringIndexOutOfBoundsException:length = 0;索引= 0                                                                                  在java.lang.String.charAt(本机方法)                                                                                  在com.miara.yagel.maspera.CreatUserActivity.isValid(CreatUserActivity.java:69)                                                                                  在com.miara.yagel.maspera.CreatUserActivity $ 1.onClick(CreatUserActivity.java:56)                                                                                  在android.view.View.performClick(View.java:6294)                                                                                  在android.view.View $ PerformClick.run(View.java:24770)                                                                                  在android.os.Handler.handleCallback(Handler.java:790)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:99)                                                                                  在android.os.Looper.loop(Looper.java:164)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:6494)                                                                                  在java.lang.reflect.Method.invoke(本机方法)                                                                                  在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

谢谢!

3 个答案:

答案 0 :(得分:0)

问题在于您的EditText的长度为0。因此,当您将其转换为String时,String的长度也为0。所以indexOf(0)会引发异常,因为您的String为空。使用isEmpty()之类的方法首先检查其是否为空,然后迭代About Indexes。

答案 1 :(得分:0)

更改您的验证方法,以便它检查空值,并在必要时修剪文本:

public static boolean isValid(String phone) {
    String str = phone.trim();
    int len = str.length();
    return ((len > 0) && ((len == 10)||(str.charAt(0) == 0 && str.charAt(1) == 5)));
}

答案 2 :(得分:0)

我在其他麻醉品中进行搜索,发现: EditText getText() returns empty string

似乎是我应该在Onclick按钮内编写第3步。 我知道我犯了小错,谢谢大家