2015-10-05 93 views
0

我正在嘗試爲edittext使用settext。我有這個代碼,因爲我需要在多個edittext中使用afterTextChanged()。如果我在案例中使用setText:應用程序崩潰,但它在一個按鈕的onClick外部正常工作。將語句設置爲switch語句中的edittext的錯誤

public Button btn; 
    public EditText et1, et2, et3; 
    private int whoHasFocus = 0; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.testing2, container, false); 

     super.onCreate(savedInstanceState); 

     et1 = (EditText) view.findViewById(R.id.ladoA); 
     et1.setOnFocusChangeListener(this); 
     et1.addTextChangedListener(this); 
     et2 = (EditText) view.findViewById(R.id.ladoB); 
     et2.setOnFocusChangeListener(this); 
     et2.addTextChangedListener(this); 
     et3 = (EditText) view.findViewById(R.id.ladoC); 
     et3.setOnFocusChangeListener(this); 
     et3.addTextChangedListener(this); 

     btn = (Button) view.findViewById(R.id.button); 
     btn.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       et1.setText("Works correctly."); 
      } 
     }); 

     return view; 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     switch (whoHasFocus) { 
      case 1: 
       et2.setText("It does not work."); 
       break; 
      case 2: 

       break; 
      case 3: 

       break; 
     } 
    } 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     switch (v.getId()) { 
      case R.id.ladoA: 
       whoHasFocus = 1; 
       break; 
      case R.id.ladoB: 
       whoHasFocus = 2; 
       break; 
      case R.id.ladoC: 
       whoHasFocus = 3; 
       break; 
     } 
    } 


} 

這裏是日誌:內

10-01 11:47:19.202 4141-4141/example.test.com.testApp D/OpenGLRenderer﹕ Enabling debug mode 0 
10-01 11:47:21.855 4141-4141/example.test.com.testApp D/dalvikvm﹕ GC_FOR_ALLOC freed 234K, 35% free 11382K/17256K, paused 26ms, total 26ms 
10-01 11:47:21.885 4141-4141/example.test.com.testApp I/dalvikvm-heap﹕ Grow heap (frag case) to 20.760MB for 6998416-byte allocation 
10-01 11:47:21.935 4141-4143/example.test.com.testApp D/dalvikvm﹕ GC_CONCURRENT freed 8K, 25% free 18209K/24092K, paused 2ms+2ms, total 40ms 
10-01 11:47:21.935 4141-4141/example.test.com.testApp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable 
10-01 11:47:21.935 4141-4141/example.test.com.testApp W/dalvikvm﹕ VFY: unable to resolve virtual method 371: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
10-01 11:47:21.935 4141-4141/example.test.com.testApp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
10-01 11:47:21.935 4141-4141/example.test.com.testApp I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity 
10-01 11:47:21.935 4141-4141/example.test.com.testApp W/dalvikvm﹕ VFY: unable to resolve virtual method 373: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
10-01 11:47:21.935 4141-4141/example.test.com.testApp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ threadid=1: stack overflow on call to Landroid/view/View;.invalidate:VZ 
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ method requires 40+20+20=80 bytes, fp is 0x57102338 (56 left) 
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ expanding stack end (0x57102300 to 0x57102000) 
10-01 11:47:28.252 4141-4141/example.test.com.testApp I/dalvikvm﹕ Shrank stack (to 0x57102300, curFrame is 0x57107d20) 
10-01 11:47:28.252 4141-4141/example.test.com.testApp D/AndroidRuntime﹕ Shutting down VM 
10-01 11:47:28.252 4141-4141/example.test.com.testApp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40e37ac8) 
10-01 11:47:28.342 4141-4141/example.test.com.testApp E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.StackOverflowError 
      at android.view.View.invalidate(View.java:10849) 
      at android.widget.TextView.invalidateRegion(TextView.java:4975) 
      at android.widget.TextView.invalidateCursor(TextView.java:4966) 
      at android.widget.TextView.spanChange(TextView.java:8060) 
      at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:10023) 
      at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979) 
      at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688) 
      at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588) 
      at android.text.Selection.setSelection(Selection.java:116) 
      at android.text.Selection.setSelection(Selection.java:127) 
      at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302) 
      at android.widget.TextView.setText(TextView.java:4092) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp.Testing2.afterTextChanged(Testing2.java:62) 
      at android.widget.TextView.sendAfterTextChanged(TextView.java:7982) 
      at android.widget.TextView.setText(TextView.java:4111) 
      at android.widget.TextView.setText(TextView.java:3924) 
      at android.widget.EditText.setText(EditText.java:104) 
      at android.widget.TextView.setText(TextView.java:3899) 
      at example.test.com.testApp 
+0

請在應用程序崩潰時發佈輸出到Logcat的錯誤。 –

+0

@AndrewBrooke泰克斯回答,我張貼在另一個答案。 –

+0

您應該將其編輯到原始文章中,而不是創建答案 –

回答

0

您正在設置afterTextChanged中的文本,這會導致再次調用相同的方法,導致無限循環。刪除偵聽器,設置文本,然後添加偵聽器。

et2.removeTextChangedListener(this); 
et2.setText("this will work"); 
et2.addTextChangedListener(this); 
0

呼叫的setText OnTextChanged導致無限循環.. 您可以設置條件來阻止它:

if (et2.getText.equals(originalValue)) { 
    // do nothing 
} else { 
    et2.setText("xxx"); 
    originalValue = et2.getText(); 
} 
1

您設置的EditText文本里面afterTextChanged,它再次調用afterTextChanged。這個遞歸調用創建了導致堆棧溢出的無限循環。