下面是我的代码,它显示了一个进度对话框来验证用户。当用户名或密码不匹配时,我想显示Toast消息(响应代码不是200)但是我得到了一个警告
WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy
和toast消息不显示
new Thread() {
public void run() {
Looper.prepare();
try {
performBackgroundProcess();
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}.start();
private void performBackgroundProcess() {
String sUserName = usernameEditText.getText().toString();
String sPassword = String authentication = sUserName + ":" + sPassword;
String login = Base64.encodeToString(authentication.getBytes(),
Base64.NO_WRAP);
Resources res = getResources();
String URLLogin = res.getString(R.string.URLlogin);
RestClient client = new RestClient(URLLogin, login);
try {
client.Execute(RequestMethod.POST);
} catch (Exception e) {
e.printStackTrace();
}
if (client.getResponseCode() != 200) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"Username or Password does not match",Toast.LENGTH_SHORT).show();
}
}
看到任何UI更改,toast,对话框,你想要的,你不能在另一个线程。你必须在主UI线程上做这个。对你来说,一个简单的方法是使用处理程序,因为现在使用aync将不得不做很多更改。
new Handler().post( new Runnable() {
public void run() {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(),"Username or Password does not match",Toast.LENGTH_SHORT).show();
}
});
}
android的处理器。操作系统不是Java
我认为你是在后台运行代码,所以吐司不会在那里工作,你必须在UI线程上写吐司而不是在后台。因为这个线程不能更新UI线程。我建议你使用AsyncTask线程,它非常干净,易于使用
而不是让作用域getApplicationcontext给出它为'this'或'ActivityName.class'并检查。