如果用户创建成功,StackMob将显示Toast

时间:2013-12-11 20:17:28

标签: java android stackmob

我有一个按钮,代码是这样的:

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 ...我该怎么做才能解决这个问题?

1 个答案:

答案 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();
                }
            }
        }
    });
}