2012-01-31 5 views
1

내 안드로이드 애플리케이션의 경우 RadioButton이 전환 될 때마다 textChangedListener을 변경해야합니다. 가능한 버튼은 4 개이며 각각에 대해 EditText 상자가 있습니다. 현재 구현 된 4 개의 버튼 중 2 개만 구현되었으며 구현 된 버튼 (예 : 버튼 1)에서 구현되지 않은 버튼으로 전환 할 때 아무런 문제가 없습니다. 그러나 다른 구현 된 버튼 (예 : 버튼 2)을 선택하려고하면 응용 프로그램이 충돌합니다.액티비티에서 textChangedListener를 바꾸는 방법

벨로우는 내 textChangedListener의 구현을 보여주는 일부 코드입니다.

public TextWatcher last; 
public TextWatcher _list1; 
public TextWatcher _list2; 

public EditText active; 
public RadioButton checked; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final EditText t1 = (EditText) findViewById(R.id.1text); 
    final EditText t2 = (EditText) findViewById(R.id.2text); 
    final EditText t3 = (EditText) findViewById(R.id.3text); 
    final EditText t4 = (EditText) findViewById(R.id.4text); 

    final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1); 
    g1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId()); 

      if (((String) checked.getText()).equalsIgnoreCase("But1")) 
      { 
       active.removeTextChangedListener(last); 
       active = t1; 
       active.setText(""); 
       active.addTextChangedListener(_list1); 
       last = _list1; 
      } 
      else if (((String) checked.getText()).equalsIgnoreCase("But2")) 
      { 
       active.removeTextChangedListener(last); 
       active = t2; 
       active.setText(""); 
       active.addTextChangedListener(_list2); 
       last = _list2; 
      } 
      else if (((String) checked.getText()).equalsIgnoreCase("But3")) 
      { 
       active = t3; 
      } 
      else if (((String) checked.getText()).equalsIgnoreCase("But4")) 
      { 
       active = t4; 
      } 
     } 
    }); 

    checked = (RadioButton)g1.findViewById(g1.getCheckedRadioButtonId()); 

    active = t1; 
    active.setText(""); 
    _list1 = (new TextWatcher(){ 
     public void afterTextChanged(Editable s) {} 
     public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 
      %Useful stuff 
     } 
    }); 
    _list2 = (new TextWatcher(){ 
     public void afterTextChanged(Editable s) {} 
     public void beforeTextChanged(CharSequence s, int start, int count, int after){} 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 
      %Different useful stuff 
     } 
    }); 
    active.addTextChangedListener(_list1); 
    last = _list1; 
} 

는 다시, 내 문제는 내가 다른 하나 TextChangedListener 전환하려고 할 때, 내 응용 프로그램에 관계없이 각 청취자 내에서 구현 무엇의 충돌이다. TextChangeListener의 특성과 구현을 간과 할 수있는 아주 명백한 것이있는 것처럼 느껴집니다. 어떤 도움을 많이 주시면 감사하겠습니다!

감사합니다.

내 오류 로그 : (참고 : 활동 이름은 "수")

01-31 08:29:17.673: INFO/ActivityManager(59): Displayed activity com.ikiar.engtools/.Number: 659 ms (total 659 ms) 
01-31 08:29:24.083: DEBUG/AndroidRuntime(276): Shutting down VM 
01-31 08:29:24.083: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main 
01-31 08:29:24.103: ERROR/AndroidRuntime(276): java.lang.ClassCastException: android.text.SpannableStringBuilder 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at com.ikiar.engtools.Number$3.onTextChanged(Number.java:122) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.TextView.sendOnTextChanged(TextView.java:6131) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2691) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2556) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.EditText.setText(EditText.java:75) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.TextView.setText(TextView.java:2531) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at com.ikiar.engtools.Number$1.onCheckedChanged(Number.java:71) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.RadioGroup.access$600(RadioGroup.java:52) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.CompoundButton.setChecked(CompoundButton.java:127) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.CompoundButton.toggle(CompoundButton.java:86) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.RadioButton.toggle(RadioButton.java:69) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.widget.CompoundButton.performClick(CompoundButton.java:98) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.view.View$PerformClick.run(View.java:8816) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.os.Handler.handleCallback(Handler.java:587) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.os.Handler.dispatchMessage(Handler.java:92) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.os.Looper.loop(Looper.java:123) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-31 08:29:24.103: ERROR/AndroidRuntime(276):  at dalvik.system.NativeStart.main(Native Method) 
01-31 08:29:24.113: WARN/ActivityManager(59): Force finishing activity com.ikiar.engtools/.Number 
01-31 08:29:24.523: INFO/ARMAssembler(59): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x3659b0:0x365a6c] in 365498 ns 
01-31 08:29:24.653: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{450232b0 com.ikiar.engtools/.Number} 
+0

logcat 출력을 확인하는 것은 어떻습니까? 충돌의 이유는 무엇입니까? 응용 프로그램이 죽으면 침묵으로 죽지 않습니다. 거기에 몇 가지 예외 또는 다른 단서가 있어야합니다. –

+0

logcat 창에 관련 내역을 포함하도록 내 게시물을 편집했습니다. – gkiar

답변

2

,이 일에 대해 내가 레이아웃 파일에 일부 문제가 있다고 생각 .. 당신은 그냥 전에이 메소드를 호출 제거되기 때문에이 방법 setText("")가 호출 될 때 리스너가 필요하지 않습니다.

내가 코드를 편집하고 main.xml에를 생성하고 텍스트 1, 텍스트 2, 텍스트 3에 1text, 2text, 3text, 4text에서 글고 ID를 변경, 텍스트 4

를 넣고 주어진 코드 것은 노력하고 있습니다. 다음

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/But1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="But1" /> 

     <EditText 
      android:id="@+id/text1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" /> 

     <RadioButton 
      android:id="@+id/But2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="But2" /> 

     <EditText 
      android:id="@+id/text2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" /> 

     <RadioButton 
      android:id="@+id/But3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="But3" /> 

     <EditText 
      android:id="@+id/text3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" /> 

     <RadioButton 
      android:id="@+id/But4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="But4" /> 

     <EditText 
      android:id="@+id/text4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:inputType="text" > 
     </EditText> 
    </RadioGroup> 

</LinearLayout> 

이 시도 main.xml에 파일의 코드 활동

public TextWatcher last; 
    public TextWatcher _list1; 
    public TextWatcher _list2; 

    public EditText active; 
    public RadioButton checked; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final EditText t1 = (EditText) findViewById(R.id.text1); 
     final EditText t2 = (EditText) findViewById(R.id.text2); 
     final EditText t3 = (EditText) findViewById(R.id.text3); 
     final EditText t4 = (EditText) findViewById(R.id.text4); 
     final Context c = getApplicationContext(); 

     final RadioGroup g1 = (RadioGroup) findViewById(R.id.radioGroup1); 
     g1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId()); 

       if (((String) checked.getText()).equalsIgnoreCase("But1")) { 
        active.removeTextChangedListener(last); 
        active = t1; 
        active.setText(""); 
        active.addTextChangedListener(_list1); 
        last = _list1; 
       } else if (((String) checked.getText()).equalsIgnoreCase("But2")) { 
        active.removeTextChangedListener(last); 
        active = t2; 
        active.setText(""); 
        active.addTextChangedListener(_list2); 
        last = _list2; 
       } else if (((String) checked.getText()).equalsIgnoreCase("But3")) { 
        active = t3; 
       } else if (((String) checked.getText()).equalsIgnoreCase("But4")) { 
        active = t4; 
       } 
      } 
     }); 

     checked = (RadioButton) g1.findViewById(g1.getCheckedRadioButtonId()); 

     active = t1; 
     active.setText(""); 
     _list1 = (new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
      } 

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

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       Toast.makeText(c, "onTextChanged called(_list1 watcher)", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     _list2 = (new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
      } 

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

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       Toast.makeText(c, "onTextChanged called(_list2 watcher)", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     active.addTextChangedListener(_list1); 
     last = _list1; 
    } 

} 


다음의 코드이다. 그 일.

+0

도움 주셔서 감사합니다. – gkiar

0

희망이 도움

하지만 다른 1

t1.addTextChangedListener(new TextWatcher(){ 

     public void afterTextChanged(Editable s) {}   

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

     public void onTextChanged(CharSequence s, int start, int before,int count) { 
      // TODO Auto-generated method stub 
          do stuff 
     }}); 

하지만 1

t1.addTextChangedListener(new TextWatcher(){ 

     public void afterTextChanged(Editable s) {}   

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

     public void onTextChanged(CharSequence s, int start, int before,int count) { 
      // TODO Auto-generated method stub 
          another stuff 
     }}); 
+0

처음에는 작동했지만, 둘 사이를 전환 할 때 청취자가 교체되지 않아 충돌이 계속 발생했지만 이제 막 덮어 쓰려고했습니다. – gkiar

0

EditText와 비슷합니다. 어떻게

if (((String) checked.getText()).equalsIgnoreCase("But1")) 
      { 
       active.setText(""); 
       active.removeTextChangedListener(last); 
       active = t1; 

       active.addTextChangedListener(_list1); 
       last = _list1; 
      } 
      else if (((String) checked.getText()).equalsIgnoreCase("But2")) 
      { 
       active.setText(""); 

       active.removeTextChangedListener(last); 
       active = t2; 
           active.addTextChangedListener(_list2); 
       last = _list2; 
      } 
+0

내가 청취자를 변경할 수 있도록 허용하는 측면에서 원래 코드와 어떤 차이가 있습니까? 감사. – gkiar

관련 문제