0

我有一个简单的消息传递应用程序,它获取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;
4

1 回答 1

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

这些顺序不应该颠倒吗?在调用它之前,您应该需要说出“消息”是getText什么。

于 2012-04-24T23:29:59.173 回答