这是我在 Android 上的代码我在 Android 上相当新,这是我的onClick
方法。每当我运行这个应用程序,然后单击按钮。它说“WordCounterApp 已停止工作”。这个应用程序用于计算用户估算了多少单词,有人可以帮忙吗?
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);
}
});
}
}