Domanda

Here is my code:

globally declared

    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(View v) ;

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

Basically the edittext line in beforeTextChanged is causing the errors. What I am actually trying to do is recording past instances of string "test" right before it is changed and creating an arraylist with them. I never managed to get any kind of string out of pastWrittenStrings yet, and I have tried everything, even converting the arraylist into an array.

Help please!

edit: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
È stato utile?

Soluzione

use

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

for showing Integer Value in EditText because setText method accept CharSequence as parameter instead of integer

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top