2013-07-26 43 views
0

這裏是我的代碼:嘗試使用由TextWatcher創建數組導致錯誤和強制關閉

全局聲明

TextView test; 
    EditText edittext; 
    TextWatcher watcher; 
    ArrayList<String> pastWrittenStrings; 

的onCreate()

....setcontentview etc... 

test = (TextView)findViewById(R.id.textView2); 
edittext = (EditText)findViewById(R.id.spokenmsg); 
pastWrittenStrings = new ArrayList<String>(); 

watcher = new TextWatcher(){ 

     @Override 
     public void afterTextChanged(Editable s) { 


     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     String writtenNoLowerCase = test.getText().toString(); 

     pastWrittenStrings.add(writtenNoLowerCase); 
     if(pastWrittenStrings.size()-1 != 0){ 
      edittext.setText(pastWrittenStrings.size()); //THIS LINE CAUSES THE ERROR 
     } 
     } 
     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

     } 

    }; 

onbuttonclick(視圖V);

test.addTextChangedListener(watcher); 
test.setText("calculations"); 

基本上,beforeTextChanged中的edittext行導致錯誤。我實際上試圖做的是在改變之前記錄字符串「test」的過去實例,並創建一個與它們一起的數組列表。我從來沒有設法從pastWrittenStrings中獲得任何類型的字符串,並且我嘗試了所有的東西,甚至將arraylist轉換爲數組。

請幫忙!

編輯:logcat的

07-26 21:12:26.555: E/AndroidRuntime(1580): FATAL EXCEPTION: main 
07-26 21:12:26.555: E/AndroidRuntime(1580): java.lang.IllegalStateException: Could not execute method of the activity 
07-26 21:12:26.555: E/AndroidRuntime(1580): Caused by: java.lang.reflect.InvocationTargetException 
07-26 21:12:26.555: E/AndroidRuntime(1580): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0 
07-26 21:13:00.500: W/dalvikvm(1948): threadid=1: thread exiting with uncaught exception (group=0x40c67930) 
07-26 21:13:00.505: E/AndroidRuntime(1948): FATAL EXCEPTION: main 
07-26 21:13:00.505: E/AndroidRuntime(1948): java.lang.IllegalStateException: Could not execute method of the activity 
07-26 21:13:00.505: E/AndroidRuntime(1948): Caused by: java.lang.reflect.InvocationTargetException 
07-26 21:13:00.505: E/AndroidRuntime(1948): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0 
07-26 21:16:21.695: W/dalvikvm(2817): threadid=1: thread exiting with uncaught exception (group=0x40c67930) 
07-26 21:16:21.705: E/AndroidRuntime(2817): FATAL EXCEPTION: main 
07-26 21:16:21.705: E/AndroidRuntime(2817): java.lang.IllegalStateException: Could not execute method of the activity 
07-26 21:16:21.705: E/AndroidRuntime(2817): Caused by: java.lang.reflect.InvocationTargetException 
07-26 21:16:21.705: E/AndroidRuntime(2817): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2 
+0

郵政logcat的。我想這是StackOverflowException,因爲任何這些方法中的.setText()都會導致SOE。看到這裏:http://stackoverflow.com/questions/7222944/changing-text-in-android-on-text-change-causes-overflow-error/7222993#7222993 只是修改它爲您的需求。 –

+0

已發佈。實際上是IllegalStateException。 –

回答

1

使用

edittext.setText(String.valueOf(pastWrittenStrings.size())); 

用於示出的EditText整數值,因爲setText方法接受CharSequence作爲參數,而不是整數

+0

你是對的。對不起,問題解決了 這只是一個非常被忽視的睡眠剝奪造成的錯誤 –

相關問題