Reputation: 29
This is my code on Android I am fairly new at Android, this is my onClick
method.
Whenever I run this app, and click the button. It says "WordCounterApp has stopped working". This app is for counting how many words a user has imputed can any one help?
package com.example.wordcountapp;
public class MainActivity extends Activity {
private TextView textView2;
private Button button1;
private EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView2 = (TextView) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = editText1.getText().toString();
String input = str;
int wordCount = -1;
String array[] = input.trim().split(" ");
if( "".equals(input) ||
"\n".equals(input)) {
wordCount = 0;
}
else{
wordCount = array.length;
}
textView2.setText(wordCount);
}
});
}
}
Upvotes: 1
Views: 107
Reputation: 5162
You can't set the value of an text view to an int, instead of textview.setText(wordCount)
use textview.setText(Integer.toString(wordCount);
Upvotes: 3
Reputation: 132972
use
textView2.setText(""+wordCount);
OR
textView2.setText(String.valueOf(wordCount));
instead of
textView2.setText(wordCount);
because you can not set Int directly to TextView you will need to convert it to String first
Upvotes: 6