- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该错误与我调用 setText() 方法以及递归调用 TextWatcher 有关。为了解决这个问题,我尝试删除观察者并在设置文本后再次添加它。我需要激活 TextWatcher 才能使我的 EditText 错误或 mPassword edittext 正常工作。如果我使用以下代码来删除和添加 TextWatcher:
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
它仍然让我崩溃。
我做错了什么?
代码如下:
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText mUsername;
private EditText mPassword;
Editable s;
private Drawable error_indicator;
String phone;
private boolean flag = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting custom drawable instead of red error indicator,
//error_indicator = getResources().getDrawable(R.drawable.ic_launcher);
int left = 0;
int top = 0;
//int right = error_indicator.getIntrinsicHeight();
//int bottom = error_indicator.getIntrinsicWidth();
phone = "";
mUsername = (EditText) findViewById(R.id.etUsername);
mPassword = (EditText) findViewById(R.id.etPassword);
// Called when user type in EditText
mUsername.addTextChangedListener(new InputValidator(mUsername));
mPassword.addTextChangedListener(new InputValidator(mPassword));
}
private class InputValidator implements TextWatcher {
private EditText et;
private InputValidator(EditText editText) {
this.et = editText;
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Toast.makeText(getApplicationContext(), "In onTextChanged() Method", Toast.LENGTH_SHORT).show();
if (s.length() != 0 && flag) {
switch (et.getId()) {
case R.id.etUsername: {
if (!Pattern.matches("(^[A-Za-z][\\w-(.|\\n)]+@[A-Za-z][\\w-(.|\\n)]+(.|\\n)(com|net|org|gov|mil|biz|info|mobi|name|in|aero|jobs|museum))$", s )) {
et.setError("please enter a valid email address");
}
}
break;
case R.id.etPassword: {
//doWork();
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
if (!Pattern.matches("\\([1-9]\\d{2}\\)[0-9]{3}-\\d{4}$", s)) {
et.setError("please enter a valid 10 digit phone number");
}
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
break;
}
}
}
else {
flag = true;
}
}
}
public String formatPhoneNumber(String phoneNumber) {
final StringBuffer sbPhone = new StringBuffer(phoneNumber);
if (sbPhone.length() == 9) {
if (sbPhone.indexOf("-") == -1) {
sbPhone.insert(8, "-");
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 9", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 8) {
if (sbPhone.indexOf("-") != -1) {
sbPhone.delete(sbPhone.indexOf("-"), sbPhone.indexOf("-"));
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 8", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 5) {
if (sbPhone.indexOf("(") != -1) {
sbPhone.delete(sbPhone.indexOf("("), sbPhone.indexOf("(")+1);
}
if (sbPhone.indexOf("(") != -1) {
sbPhone.delete(sbPhone.indexOf(")"), sbPhone.indexOf(")"));
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 5", Toast.LENGTH_SHORT).show();
}
else if (sbPhone.length() == 3) {
if (sbPhone.indexOf("(") == -1) {
sbPhone.insert(0, "(");
}
if (sbPhone.indexOf(")") == -1 && sbPhone.indexOf("(") != -1) {
sbPhone.append(")");
}
Toast.makeText(getApplicationContext(), "In formatPhoneNumber() Method - 3", Toast.LENGTH_SHORT).show();
}
return sbPhone.toString();
}
}
错误:
03-18 11:59:51.910: E/AndroidRuntime(24049): FATAL EXCEPTION: main
03-18 11:59:51.910: E/AndroidRuntime(24049): java.lang.StackOverflowError
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.text.DynamicLayout.reflow(DynamicLayout.java:324)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.makeSingleLayout(TextView.java:7106)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.makeNewLayout(TextView.java:6955)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.checkForRelayout(TextView.java:7551)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4410)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.sendOnTextChanged(TextView.java:8430)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4413)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4247)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.EditText.setText(EditText.java:108)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.setText(TextView.java:4222)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity.formatPhoneNumber(MainActivity.java:187)
03-18 11:59:51.910: E/AndroidRuntime(24049): at com.example.edittexttest.MainActivity$InputValidator.onTextChanged(MainActivity.java:105)
03-18 11:59:51.910: E/AndroidRuntime(24049): at android.widget.TextView.se
感谢任何帮助。
最佳答案
您在删除或添加时创建了两个不同的监听器对象。
mPassword.removeTextChangedListener(new InputValidator(mPassword));
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(new InputValidator(mPassword));
这一定是
添加成员为
private InputValidator mPasswordTextWatcher = null;
private InputValidator mUserNameTextWatcher = null;
在onCreate()
mPasswordTextWatcher = new InputValidator(mPassword);
mUserNameTextWatcher = new InputValidator(mUsername);
// Called when user type in EditText
mUsername.addTextChangedListener(mUserNameTextWatcher);
mPassword.addTextChangedListener(mPasswordTextWatcher);
现在就这样做
mPassword.removeTextChangedListener(mPasswordTextWatcher);
phone = mPassword.getText().toString();
phone = formatPhoneNumber(phone);
Log.i("PHONE", "Phone NUMB IS:"+phone);
mPassword.setText(phone);
flag = false;
mPassword.addTextChangedListener(mPasswordTextWatcher);
对用户名 EditText 执行相同操作。
关于java - Stackoverflow EditText 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487038/
使用 htaccess 我能够隐藏文件扩展名,使其成为/home 而不是/home.html。但是,对于主页,我想完全隐藏文件名,例如 stackoverflow.com,而不是 stackoverf
我试图在关闭子窗体时关闭我的主(父)窗体。然而,这给了我一个 StackOverflow 异常。 但是,如果我在 FormClosed 事件上调用 _child.Dispose,它会按预期工作。我应该
我在 IDEA 中使用 Gradle 并得到以下输出。 * What went wrong: A problem occurred evaluating script. > java.lang.Sta
我是一个 java 新手,并且在 StackOverflow 错误/在类之间访问文件的能力方面遇到了一个非常令人困惑的问题。我知道根本原因可能是我进行了一些递归调用,但修复它的语法却让我无法理解。我认
我需要编写一个程序来生成一些随机数,而不让两个数字落在某个范围内。不幸的是,我需要在学校做这件事,而我在计算机上唯一可以使用的语言是 Java(否则我会用 C++ 来做)。我对 Java 知之甚少,我
尝试设置一个 Person 类 我在尝试将信息封装在类中以使其不会被意外更改时遇到了问题。除了当我尝试使用 setter/getters 进行封装时,该类工作得非常好。我认为问题是这些方法最终会互相循
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
我正在开发一个学校项目,该项目必须有 3 个带 1 个指针的快速排序、3 个带 2 个指针和 2 个堆的快速排序。 到目前为止,我已经编写了快速排序和堆之一。我遇到的问题是快速排序在小情况下工作正常,
我正在制作一个程序,它生成列表的所有子集,然后排列所有这些子集。我有一个生成子集的方法和一个生成排列的方法。但是,它们不能一起工作,即当我排列子集时,我没有得到子集的所有排列,并且出现 StackOv
我有以下函数,它应该产生笛卡尔平面中的所有坐标,我可以在n步中从原点到达: 原点是“位置”,步数是“强度”,它是一个 1-10 的整数。但是,我不断收到 stackoverflow 错误。每次我调用它
当我尝试使用 scala lib(使用 re2)匹配上述正则表达式时,代码进入以下路径并超时 1 分钟: 正则表达式: (([a-z0-9!#$%&'*+?^_`{|}~-]+(?:.[a-z0-9!
我正在开发一个运输 map 项目。我为该位置创建了一个名为 Chart 的对象和一个名为 Pos 的对象。我无法开始工作的是,当我从文本文件读取图表以正确创建对象 Pos 时,因为这就是失败的地方。
public class SortAnimal { public static Comparator getAnimalTagComparator() { return
我因使用以下泛型而收到 StackOverflowError: 积分: package me.expdev.gkitpvp; /* * Project created by ExpDev */ i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我为一些命题逻辑表达式编写了一个javaCC解析器。表达式可能会很长,有 30k 个字符。 当我解析如此大的表达式时,出现堆栈溢出异常。 是否有一些虚拟机参数决定堆栈大小? 或者在这种情况下你会怎么做
这个问题已经有答案了: What is a StackOverflowError? (15 个回答) 已关闭 8 年前。 最高数字double可以代表极高,我想。尽管下面的代码会抛出异常。这实际上是我
我正在为我的 API 构建一个 SDK,其中有两个模块,分别是 A 类和 B 类。 public class A{ public final B = new B(); public f
执行此递归时,我收到堆栈溢出错误。有一个模式,它第一次说: at MazeGui.move(MazeGui.java:79) which is line if(rigting.goal == true
我编写的这段代码在达到一定的输入大小之前效果很好。如果输入太大,我会收到“java.lang.StackOverflowError”。我已经阅读了 stackoverflow 上有关此主题的其他一些条
我是一名优秀的程序员,十分优秀!