2014-03-18 63 views
0

錯誤與我調用setText()方法和調用TextWatcher遞歸。爲了解決這個問題,我嘗試刪除觀察者並在設置文本後再次添加它。爲了使我的EditText錯誤或mPassword編輯文本生效,我需要TextWatcher處於活動狀態。如果我使用下面的代碼刪除並添加TextWatcher:Stackoverflow EditText Widget

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)][email protected][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 

任何幫助表示讚賞。

+0

這是哪一行? 'formatPhoneNumber(MainActivity.java:187)' – njzk2

回答

1

您在刪除或添加時創建兩個不同的偵聽器對象。

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做同樣的處理。

+0

當我這樣做時,它仍然崩潰。 – user268397