Android进度条未显示

时间:2011-04-28 05:47:25

标签: android progress-bar

private Handler handler = new Handler()
{
    @Override
    public void handleMessage(Message msg)
    {
        removeDialog(0);
        switch (msg.what)
        {
            case SUCCESS:
                Log.i("LoginActivity", "Got inputstream");
                final InputStream is = (InputStream)msg.obj;
                if (is != null)
                {
                    //TODO: examine the input stream, etc...
                }
                break;
            case FAILURE:
                Log.i("LoginActivity", "Login failed");
                //TODO: Failure handling
                break;
            default:
                break;
        }
    }
};

@Override
protected Dialog onCreateDialog(int id)
{
    switch (id)
    {
        case 0:
        {
            dialog = new ProgressDialog(this);
            dialog.setMessage("Please wait...");
            dialog.setIndeterminate(true);
            dialog.setCancelable(true);
            return dialog;
        }
    }
    return null;
}

public void loginRequest(String name,String password)
{   
    showDialog(0);
    loginThread thread = new loginThread(serverURL, name, password,getMyIMENumber(), handler);
    thread.start();
}

...............

public class loginThread extends Thread {

String name;
String password;
InputStream is;
String Url;
String ime;

private final Handler handler;

   public loginThread(String Url,String name,String password,String ime,final Handler handler) {
       this.name = name;
       this.Url = Url;
       this.password = password;
       this.ime = ime;
       this.handler = handler;         
   }


   public void run() {
          try{
                  HttpClient httpClient = new DefaultHttpClient();
                  String loginURL = Url+"member/login";
                  HttpPost httppost = new HttpPost(loginURL);
                  HttpParams httpParameters = new BasicHttpParams();
                  List<NameValuePair> pairs = new ArrayList<NameValuePair>();
                  if(name.contentEquals(""))
                  {
                      pairs.add(new BasicNameValuePair("ime", ime));
                  }
                  else
                  {
                      pairs.add(new BasicNameValuePair("userName", name));
                      pairs.add(new BasicNameValuePair("password", password));                            
                  }                  


                  int timeoutConnection = 3000;
                  HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                  // Set the default socket timeout (SO_TIMEOUT) 
                  // in milliseconds which is the timeout for waiting for data.
                  httppost.setEntity(new UrlEncodedFormEntity(pairs));
                  HttpResponse response = httpClient.execute(httppost);                           
                  HttpEntity entity = response.getEntity();
                  final InputStream is = entity.getContent();

                  Message message = new Message();
                  message.obj = is;
                  message.what = server.SUCCESS;
                  handler.sendMessage(message);
          }catch(Exception e){  
              Log.e("log_tag", "Error in http connection " + e.toString(), e);
              handler.sendEmptyMessage(server.FAILURE);
          }           

   }

}

进度对话框没有显示我在这里做错了什么!!请帮帮我..

......................

//从DexFile.java编译(版本1.5:49.0,超级位) 公共最终类dalvik.system.DexFile {

//方法描述符#8(Ljava / io / File;)V   //堆栈:3,当地人:2   public DexFile(java.io.File文件)抛出java.io.IOException;      0 aload_0 [this]      1 invokespecial java.lang.Object()[1]      4个新的java.lang.RuntimeException [2]      7重复      8 ldc [3]     10 invokespecial java.lang.RuntimeException(java.lang.String)[4]     13岁       行号:         [pc:0,line:4]       局部变量表:         [pc:0,pc:14] local:此索引:0类型:dalvik.system.DexFile         [pc:0,pc:14] local:文件索引:1类型:java.io.File

//方法描述符#18(Ljava / lang / String;)V   //堆栈:3,当地人:2   public DexFile(java.lang.String fileName)抛出java.io.IOException;      0 aload_0 [this]      1 invokespecial java.lang.Object()[1]      4个新的java.lang.RuntimeException [2]      7重复      8 ldc [3]     10 invokespecial java.lang.RuntimeException(java.lang.String)[4]     13岁       行号:         [pc:0,行:5]       局部变量表:         [pc:0,pc:14] local:此索引:0类型:dalvik.system.DexFile         [pc:0,pc:14] local:fileName index:1 type:java.lang.String

//方法描述符#22(Ljava / lang / String; Ljava / lang / String; I)Ldalvik / system / DexFile;   //堆栈:3,当地人:3   public static dalvik.system.DexFile loadDex(java.lang.String sourcePathName,java.lang.String outputPathName,int flags)抛出java.io.IOException;      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:6]       局部变量表:         [pc:0,pc:10] local:sourcePathName index:0 type:java.lang.String         [pc:0,pc:10] local:outputPathName index:1 type:java.lang.String         [pc:0,pc:10] local:flags index:2 type:int

//方法描述符#28()Ljava / lang / String;   //堆栈:3,本地人:1   public java.lang.String getName();      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:7]       局部变量表:         [pc:0,pc:10] local:这个索引:0类型:dalvik.system.DexFile

//方法描述符#30()V   //堆栈:3,本地人:1   public void close()抛出java.io.IOException;      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:8]       局部变量表:         [pc:0,pc:10] local:这个索引:0类型:dalvik.system.DexFile

//方法描述符#32(Ljava / lang / String; Ljava / lang / ClassLoader;)Ljava / lang / Class;   //堆栈:3,当地人:3   public java.lang.Class loadClass(java.lang.String name,java.lang.ClassLoader loader);      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:9]       局部变量表:         [pc:0,pc:10] local:此索引:0类型:dalvik.system.DexFile         [pc:0,pc:10] local:name index:1 type:java.lang.String         [pc:0,pc:10] local:loader index:2 type:java.lang.ClassLoader

//方法描述符#37()Ljava / util / Enumeration;   //签名:()Ljava / util / Enumeration;   //堆栈:3,本地人:1   public java.util.Enumeration entries();      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:10]       局部变量表:         [pc:0,pc:10] local:这个索引:0类型:dalvik.system.DexFile

//方法描述符#30()V   //堆栈:3,本地人:1   protected void finalize()抛出java.io.IOException;      0新的java.lang.RuntimeException [2]      3个重复      4 ldc [3]      6 invokespecial java.lang.RuntimeException(java.lang.String)[4]      9个人       行号:         [pc:0,行:11]       局部变量表:         [pc:0,pc:10] local:这个索引:0类型:dalvik.system.DexFile

//方法描述符#42(Ljava / lang / String;)Z   public static native boolean isDexOptNeeded(java.lang.String arg0)抛出java.io.FileNotFoundException,java.io.IOException; }

3 个答案:

答案 0 :(得分:3)

请勿拨打thread.run()thread.start()

run()在同一个线程上调用函数run()start()将在新主题中调用它。目前,GUI线程被阻止,因为它执行的操作应该在后台线程中完成。

您可以通过以下方式调用该主题:

Thread thread = new Thread(new loginThread(serverURL, name, password,getMyIMENumber()));
thread.start();

或者让您的loginThread 扩展线程,而不是实现Runnable。

答案 1 :(得分:2)

您可以立即从线程中检索InputStream,它很可能仍为空,或者具有初始值,但尚未得到http响应。这样,您的进度对话框最多显示几毫秒。

在调用loginRequest后结束thread.start(),如果您使用run获得了结果或对UI线程的任何异常,则从您的线程的Handler方法中进行通信。在处理程序的handleMessage中检查loginRequest的结果(结果输入流最好直接传递给处理程序,而不是从线程中检索它),并删除进度对话框。

<强>更新 以下是如何使用线程实现所需内容的示例:
在下面的代码中,您的主要活动被引用为LoginActivity(因为我不知道您的类的名称),因此您应该将其更改为java活动类的名称,您拥有{ {1}}方法。

从主要活动中移除loginRequest方法,然后输入:

showProgressDialog()

请注意,public static final int FAILURE = 0; public static final int SUCCESS = 1; private ProgressDialog dialog; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { removeDialog(0); switch (msg.what) { case SUCCESS: Log.i("LoginActivity", "Got inputstream"); final InputStream is = (InputStream)msg.obj; if (is != null) { //TODO: examine the input stream, etc... } break; case FAILURE: Log.i("LoginActivity", "Login failed"); //TODO: Failure handling break; default: break; } } }; @Override protected Dialog onCreateDialog(int id) { switch (id) { case 0: { dialog = new ProgressDialog(this); dialog.setMessage("Please wait..."); dialog.setIndeterminate(true); dialog.setCancelable(true); return dialog; } } return null; } public void loginRequest(String name, String password) { showDialog(0); LoginThread thread = new LoginThread(serverURL, name, password, getMyIMENumber(), handler); thread.start(); } 方法已更改!
您的loginRequest课程也是如此:

LoginThread

现在,您的public class LoginThread extends Thread { private final String name; private final String password; private final String url; private final String ime; private final Handler handler; public LoginThread(String Url, String name, String password, String ime, final Handler handler) { this.name = name; this.url = Url; this.password = password; this.ime = ime; this.handler = handler; } public void run() { try { HttpClient httpClient = new DefaultHttpClient(); String loginURL = url + "member/login"; HttpPost httppost = new HttpPost(loginURL); HttpParams httpParameters = new BasicHttpParams(); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); if (name.contentEquals("")) { pairs.add(new BasicNameValuePair("ime", ime)); } else { pairs.add(new BasicNameValuePair("userName", name)); pairs.add(new BasicNameValuePair("password", password)); } int timeoutConnection = 3000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. httppost.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = httpClient.execute(httppost); HttpEntity entity = response.getEntity(); final InputStream is = entity.getContent(); Message message = new Message(); message.obj = is; message.what = LoginActivity.SUCCESS; handler.sendMessage(message); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString(), e); handler.sendEmptyMessage(LoginActivity.FAILURE); } } } 实例有一个LoginThread类型的新成员。它通过构造函数参数(与其他参数)初始化,因此您可以将其声明为Handler 此处理程序将通知您的UI线程(主要活动),是否已从http响应获取输入流,或者由于某种原因它是否失败。
完成final实施,让它做你需要的。

我希望代码能够清除这种方法。

答案 2 :(得分:0)

除非并且直到您调用进度对话框的show方法,否则它将不会显示。

do this in your showProgressDialog method -- mDialog.show();