尝试使用TextWatcher创建的数组会导致错误和强制关闭

时间:2013-07-26 18:24:19

标签: java android arrays 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”的过去实例并使用它们创建一个arraylist。我从来没有设法从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

1 个答案:

答案 0 :(得分:1)

使用

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

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