我有一个按钮,代码是这样的:
public void buttonRegister(View view){
EditText etUsername = (EditText) findViewById(R.id.etUsername);
EditText etPassword = (EditText) findViewById(R.id.etPassword);
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
User newUser = new User(username, password);
newUser.save(new StackMobModelCallback() {
@Override
public void failure(StackMobException arg0) {
// TODO Auto-generated method stub
System.out.println("fail");
Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
}
@Override
public void success() {
// TODO Auto-generated method stub
System.out.println("success");
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
});
}
但应用程序没有显示Toast ...我该怎么做才能解决这个问题?
答案 0 :(得分:1)
您可能需要执行Toast
中的UI tread
代码。像这样:
public void buttonRegister(View view){
EditText etUsername = (EditText) findViewById(R.id.etUsername);
EditText etPassword = (EditText) findViewById(R.id.etPassword);
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
User newUser = new User(username, password);
newUser.save(new StackMobModelCallback() {
@Override
public void failure(StackMobException arg0) {
// TODO Auto-generated method stub
System.out.println("fail");
YourActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(YourActivity.this,
"Fail",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void success() {
// TODO Auto-generated method stub
System.out.println("success");
YourActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(YourActivity.this,
"Success",
Toast.LENGTH_SHORT).show();
}
}
}
});
}