0

这是我的代码:

全球宣布

    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”被更改之前记录它的过去实例,并用它们创建一个数组列表。我从来没有设法从过去的WrittenStrings 中得到任何类型的字符串,我已经尝试了一切,甚至将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
4

1 回答 1

1

利用

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

用于在 EditText 中显示整数值,因为setText方法接受CharSequence作为参数而不是整数

于 2013-07-26T18:28:03.860 回答