2 o19830015 o19830015 于 2012.12.25 11:14 提问

onClickActionListener的问题

我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);   
            }
        });
    }
}

1个回答

cytown
cytown   2012.12.25 12:01
已采纳
textView2.setText(wordCount + "");
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!