我onClick方法的代碼,實現功能是計算用戶輸入的字符數。每次我運行點擊按鈕之後,都會說WordCounterApp has stopped working
。多謝幫忙解決。
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);
}
});
}
}
textView2.setText(wordCount + "");