showDialog为什么无法显示对话框?

当我把线程去掉的时候对话框就可以显示?
代码如下:

package org.yuqidi.hrsystem;

import static org.yuqidi.hrsystem.Util.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends Activity {
CheckBox checkBox;
Button btLogin;
Button btReturn;
ProgressDialog pd;
Connector connector;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
checkBox=(CheckBox)findViewById(R.id.cbRemember);
btLogin=(Button)findViewById(R.id.btLogin2);
btLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pd = ProgressDialog.show(LoginActivity.this, "请稍候", "正在连接服务器...", true, true);
// try {
// Thread.sleep(5000);
login();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

        }
    });
    btReturn=(Button)findViewById(R.id.btReturn);
    btReturn.setOnClickListener(new View.OnClickListener(){
        Intent intent;
        public void onClick(View v) {
            android.os.Process.killProcess(android.os.Process.myPid()); 
            intent=new Intent(LoginActivity.this,org.yuqidi.hrsystem.indexActivity.class);
            startActivity(intent);
            finish();
        }
    });
}
public void login() {
    new Thread(){
        public void run() {
            Looper.prepare();
                try{
                    if(connector==null){
                    connector=new Connector(ADDRESS, PORT);
                    }
                    EditText etUid=(EditText)findViewById(R.id.etUid);
                    EditText etPwd=(EditText)findViewById(R.id.etPwd);
                    String uid=etUid.getEditableText().toString().trim();
                    String pwd=etPwd.getEditableText().toString().trim();
                    String message="<#LOGIN#>"+uid+"|"+pwd;
                    System.out.println(message);
                    connector.dout.writeUTF(message);
                    int serverMessage = connector.din.readInt();
                    pd.dismiss(); 
                    showDialog(serverMessage);
                    System.out.println("+++++++++1"+serverMessage);
                    if(serverMessage==0){
                        showDialog(serverMessage);
                        Looper.loop();
                        Looper.myLooper().quit();
                    }else{
                        System.out.println("+++++++++1"+serverMessage);
                        showDialog(2);
                        System.out.println("+++++++++1"+"serverMessage");
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }.start();
}
protected Dialog onCreateDialog(int id) {                   
    Dialog dialog = null;   
    Builder builder=null;
    switch(id){ 
    case 0:
        builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.app_icon);   
        builder.setMessage(R.string.loginfail);
        builder.setPositiveButton(R.string.btnOk, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog = builder.create();          
        break;
    case 1:
        builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.app_icon);       
        builder.setMessage(R.string.alertepl);  
        builder.setPositiveButton(R.string.btnOk, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog = builder.create();          
        break;
    case 2:
        builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.app_icon);   
        builder.setMessage(R.string.alertmng);
        builder.setPositiveButton(R.string.btnOk, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog = builder.create();          
        break;
    case 3:
        builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.app_icon);   
        builder.setMessage(R.string.empty);
        builder.setPositiveButton(R.string.btnOk, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton(R.string.btnCancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        dialog = builder.create();          
        break;
    default:
        break;
    }
    return dialog;  

}
protected void onDestroy() {
    if(connector != null){
        connector.sayBye();
    }
    super.onDestroy();
}

}

2个回答

建议放到Handler去处理,结合HandlerThread和Message.

看你的代码应该是由异常或者是不满足条件的时候给出提示,应该是有一个return的动作,可能跟你的代码逻辑有关系,建议再考虑一下实现。 :idea:

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!