OnClick方法杀死android应用程序

时间:2012-04-24 23:23:33

标签: java android eclipse logcat

我有一个简单的消息传递应用程序,它获取editText框的文本并将其发送到服务器。我想要做的就是当文本被发送到服务器时,我希望editText框重置。

这是我的代码,但它不会重置editText框:

public void sendMessage(View v) {
        Editable messagetext;

        messagetext = message.getText();

        final SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getBaseContext());

        username = prefs.getString("username", "null");

        where = prefs.getString("chat", "null");
        message = (EditText) findViewById(R.id.inptbox);

        function = new Functions();

        response = function
                .sendMessage(username, where, messagetext.toString());

    }

如果我再添加一行代码,为了重置该框,我的应用程序就会死掉:

public void sendMessage(View v) {
    Editable messagetext;

    messagetext = message.getText();
        message.setText("");
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    username = prefs.getString("username", "null");

    where = prefs.getString("chat", "null");
    message = (EditText) findViewById(R.id.inptbox);

    function = new Functions();

    response = function
            .sendMessage(username, where, messagetext.toString());

}

我得到的错误(忍受我,我不是那么好的logcat)是:

E/AndroidRuntime(7207): java.lang.IllegalStateException: Could not execute method of the activity

全局变量列表:

String username = "";
    Functions function;
    EditText message;
    String response = "";
    String where = "";
    String inboxx;

1 个答案:

答案 0 :(得分:2)

...
messagetext = message.getText();                 
...
message = (EditText) findViewById(R.id.inptbox);  
...

这些顺序不应该被颠倒吗?您应该在致电getText之前说明“消息”是什么。