2012-06-27 4 views
0

이 코드에는 두 가지 문제점이 있습니다. 이전 질문에서 좋은 사람들 덕분에 제대로 작동하게되었지만 지금은 무한 루프에서 다시 볼 수 있으며 어디서 오는지 알 수 없습니다. 여기서 행하려고하는 것은 행맨 게임입니다 : EditText (리터럴)로 읽은 한 문자를 처리하고 한 단어 (cuvAles)로 검색 한 다음 밑줄을 해당 문자로 바꿉니다.Android : EditText의 무한 루프 TextWatcher

1) String spatii2 = spatii.substring(0, poz*2-1) + ghici + spatii.substring(poz*2+1, spatii.length()-1);가 StringIndexOutOfBounds 예외가 발생합니다 :

litera.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){ 
       String ghici = litera.getText().toString(); 

       if(!ghici.equals("")){ 
       System.out.println(ghici); 
       litera.setText(""); 

       if(cuvAles.contains(ghici)){ 
        int poz = 0; 
        while(cuvAles.indexOf(ghici, poz)!= -1){ 
         poz = cuvAles.indexOf(ghici); 
         String spatii = cuvant.getText().toString(); 
         String spatii2 = spatii.substring(0, poz*2-1) + ghici + spatii.substring(poz*2+1, spatii.length()-2); 
         cuvant.setText(spatii2); 
        } 
       } 
       else gresite.append(ghici+" "); 
       } 
      } 
     }); 

여기에 두 가지 문제가 있습니다

다음은 문제의 기능입니다. 나는 그것이 spatii.length() 부분이라고 생각하지만 -2로 시도했지만 여전히 작동하지 않습니다. 단어가 밑줄과 일치하지 않는 이유는 내가 그들 사이에 공백이 있어야한다는 것입니다.

2) 다른 문제를 제거하면 (문자열을 상수로 바꾸면) 무한 루프가 발생합니다 (프로그램이 응답을 멈추고 logcat에서 GC가 광란하게 작동하기 때문에 무한 루프라고 생각합니다) . 그것은 당신의 텍스트 변경 리스너를 호출 계속됩니다로 글고 치기를 업데이트하기 전에

답변

1

제거 텍스트 변경 리스너 .. 작동하지 않습니다 불행히도

litera.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){ 
       String ghici = litera.getText().toString(); 

    litera.removeTextChangedListener(this);    


       if(!ghici.equals("")){ 
       System.out.println(ghici); 
       litera.setText(""); 

       if(cuvAles.contains(ghici)){ 
        int poz = 0; 
        while(cuvAles.indexOf(ghici, poz)!= -1){ 
         poz = cuvAles.indexOf(ghici); 
         String spatii = cuvant.getText().toString(); 
         String spatii2 = spatii.substring(0, poz*2-1) + ghici + spatii.substring(poz*2+1, spatii.length()-2); 
         cuvant.setText(spatii2); 
        } 
       } 
       else gresite.append(ghici+" "); 
       } 
      } 
     }); 
+0

. 다른 아이디어? 그리고 문자열 인덱스가 범위를 벗어났습니다. – FloIancu