Android 安卓:Can';t在未调用Looper.prepare()的线程内创建处理程序
可能重复:Android 安卓:Can';t在未调用Looper.prepare()的线程内创建处理程序,android,multithreading,handler,looper,Android,Multithreading,Handler,Looper,可能重复: 在我的程序中,我想要显示进度对话框,因为我已经创建了线程,但它给了我无法在未调用Looper的线程内创建处理程序。prepare()我也查看了前面发布的问题,但我可以处理这个问题。这是我的密码 dialog=ProgressDialog.show(Login.this, "Connecting to the Server", "Please wait"); Thread thred=new Thread(new Runnable() {
在我的程序中,我想要显示进度对话框,因为我已经创建了线程,但它给了我
无法在未调用Looper的线程内创建处理程序。prepare()
我也查看了前面发布的问题,但我可以处理这个问题。这是我的密码
dialog=ProgressDialog.show(Login.this, "Connecting to the Server", "Please wait");
Thread thred=new Thread(new Runnable() {
public void run() {
try {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.diskonbanget.com/bni/login/login.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
if(str.toString().equalsIgnoreCase("false"))
{
dialog.dismiss();
AlertDialog alert = new AlertDialog.Builder(Login.this).create();
alert.setMessage("Please enter valid username & Password");
alert.setButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
}
});
alert.setIcon(R.drawable.loginlogo);
alert.show();
}
else{
Intent intent=new Intent(Login.this,MainMenu.class);
intent.putExtra("photo", str);
intent.putExtra("username", username);
txtusername.setText("");
txtPassword.setText("");
dialog.dismiss();
startActivity(intent);
}
} catch (Exception e) {
dialog.dismiss();
AlertDialog alert = new AlertDialog.Builder(Login.this).create();
alert.setMessage("Can not Connect to the server.please make sure your internet is Switch on");
alert.setButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
}
});
alert.setIcon(R.drawable.loginlogo);
alert.show();
}
}
});
thred.start();
dialog=ProgressDialog.show(Login.this,“连接到服务器”,“请稍候”);
Thread thred=新线程(new Runnable(){
公开募捐{
试一试{
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.diskonbanget.com/bni/login/login.php");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
字符串str=inputStreamToString(response.getEntity().getContent()).toString();
if(str.toString().equalsIgnoreCase(“false”))
{
dialog.dismise();
AlertDialog alert=新建AlertDialog.Builder(Login.this.create();
alert.setMessage(“请输入有效的用户名和密码”);
alert.setButton(“确定”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
});
alert.setIcon(R.drawable.loginlogo);
alert.show();
}
否则{
意图=新意图(Login.this,main menu.class);
意图。额外(“照片”,str);
intent.putExtra(“用户名”,用户名);
txtusername.setText(“”);
txtPassword.setText(“”);
dialog.dismise();
星触觉(意向);
}
}捕获(例外e){
dialog.dismise();
AlertDialog alert=新建AlertDialog.Builder(Login.this.create();
alert.setMessage(“无法连接到服务器。请确保您的internet已打开”);
alert.setButton(“确定”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
});
alert.setIcon(R.drawable.loginlogo);
alert.show();
}
}
});
thred.start();
请有人帮我解决这个问题。后台线程无法显示
对话框。请让主应用程序线程执行此操作,例如使用AsyncTask
而不是原始线程,并在onPostExecute()
中显示对话框,该代码在应用程序中的何处?一项活动、一项服务、你自己的POJO、其他地方?将信息发送到UI的最佳方式取决于您的位置…此代码位于我的活动类中