我有一个简单的消息应用程序,它获取 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;
最佳答案
...
messagetext = message.getText();
...
message = (EditText) findViewById(R.id.inptbox);
...
这些的顺序不应该颠倒吗?在调用 getText
之前,您应该需要说明“消息”是什么。
关于java - OnClick 方法杀死 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10307368/