简体   繁体   English

Android 进度条未显示

[英]Android Progress bar not showing up

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);
          }           

   }

} }

Progress dialog doesn't show up what am i doing wrong here.. please help me..进度对话框没有显示我在这里做错了什么..请帮助我..

...................... ……………………………………………………………………………………………………………………

// Compiled from DexFile.java (version 1.5: 49.0, super bit) public final class dalvik.system.DexFile { // 编译自 DexFile.java (version 1.5: 49.0, super bit) public final class dalvik.system.DexFile {

// Method descriptor #8 (Ljava/io/File;)V // Stack: 3, Locals: 2 public DexFile(java.io.File file) throws java.io.IOException; // 方法描述符 #8 (Ljava/io/File;)V // Stack: 3, Locals: 2 public DexFile(java.io.File file) throws java.ZF98ED07A4D5F50F7DE1410D; 0 aload_0 [this] 1 invokespecial java.lang.Object() [1] 4 new java.lang.RuntimeException [2] 7 dup 8 ldc [3] 10 invokespecial java.lang.RuntimeException(java.lang.String) [4] 13 athrow Line numbers: [pc: 0, line: 4] Local variable table: [pc: 0, pc: 14] local: this index: 0 type: dalvik.system.DexFile [pc: 0, pc: 14] local: file index: 1 type: java.io.File 0 aload_0 [this] 1 invokespecial java.lang.Object() [1] 4 new java.lang.RuntimeException [2] 7 dup 8 ldc [3] 10 invokespecial java.lang.RuntimeException(java.lang.String) [4 ] 13 athrow 行号:[pc:0, line:4] 局部变量表:[pc:0, pc:14] local: this index: 0 type: dalvik.system.DexFile [pc: 0, pc: 14]本地:文件索引:1 类型:java.io.File

// Method descriptor #18 (Ljava/lang/String;)V // Stack: 3, Locals: 2 public DexFile(java.lang.String fileName) throws java.io.IOException; // 方法描述符 #18 (Ljava/lang/String;)V // Stack: 3, Locals: 2 public DexFile(java.lang.String fileName) throws java.io.IOException; 0 aload_0 [this] 1 invokespecial java.lang.Object() [1] 4 new java.lang.RuntimeException [2] 7 dup 8 ldc [3] 10 invokespecial java.lang.RuntimeException(java.lang.String) [4] 13 athrow Line numbers: [pc: 0, line: 5] Local variable table: [pc: 0, pc: 14] local: this index: 0 type: dalvik.system.DexFile [pc: 0, pc: 14] local: fileName index: 1 type: java.lang.String 0 aload_0 [this] 1 invokespecial java.lang.Object() [1] 4 new java.lang.RuntimeException [2] 7 dup 8 ldc [3] 10 invokespecial java.lang.RuntimeException(java.lang.String) [4 ] 13 athrow 行号:[pc:0,line:5] 局部变量表:[pc:0,pc:14] local:this index:0 type:dalvik.system.DexFile [pc:0,pc:14]本地:文件名索引:1 类型:java.lang.String

// Method descriptor #22 (Ljava/lang/String;Ljava/lang/String;I)Ldalvik/system/DexFile; // 方法描述符 #22 (Ljava/lang/String;Ljava/lang/String;I)Ldalvik/system/DexFile; // Stack: 3, Locals: 3 public static dalvik.system.DexFile loadDex(java.lang.String sourcePathName, java.lang.String outputPathName, int flags) throws java.io.IOException; // Stack: 3, Locals: 3 public static dalvik.system.DexFile loadDex(java.lang.String sourcePathName, java.lang.String outputPathName, int flags) throws java.io.IOException; 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 6] Local variable table: [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 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:]表:[pc:0,pc:10] 本地:sourcePathName 索引:0 类型:java.lang.String [pc:0,pc:10] 本地:outputPathName 索引:1 类型:Z93F725A07423FE1C889F448B33D20F46Z.lang.String [pc: , pc: 10] 本地:标志索引:2 类型:int

// Method descriptor #28 ()Ljava/lang/String; // 方法描述符 #28 ()Ljava/lang/String; // Stack: 3, Locals: 1 public java.lang.String getName(); // Stack: 3, Locals: 1 public java.lang.String getName(); 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 7] Local variable table: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:]表:[pc:0,pc:10] 本地:此索引:0 类型:dalvik.system.DexFile

// Method descriptor #30 ()V // Stack: 3, Locals: 1 public void close() throws java.io.IOException; // 方法描述符 #30 ()V // Stack: 3, Locals: 1 public void close() throws java.io.IOException; 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 8] Local variable table: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:] 局部变量表:[pc:0,pc:10] 本地:此索引:0 类型:dalvik.system.DexFile

// Method descriptor #32 (Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/Class; // 方法描述符 #32 (Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/Class; // Stack: 3, Locals: 3 public java.lang.Class loadClass(java.lang.String name, java.lang.ClassLoader loader); // Stack: 3, Locals: 3 public java.lang.Class loadClass(java.lang.String name, java.lang.ClassLoader loader); 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 9] Local variable table: [pc: 0, pc: 10] local: this index: 0 type: 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 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:]表:[pc:0,pc:10] 本地:此索引:0 类型:dalvik.system.DexFile [pc:0,pc:10] 本地:名称索引:1 类型:java.lang.String [pc:0 , pc: 10] 本地:加载程序索引:2 类型:java.lang.ClassLoader

// Method descriptor #37 ()Ljava/util/Enumeration; // 方法描述符 #37 ()Ljava/util/Enumeration; // Signature: ()Ljava/util/Enumeration; // 签名:()Ljava/util/Enumeration; // Stack: 3, Locals: 1 public java.util.Enumeration entries(); // Stack: 3, Locals: 1 public java.util.Enumeration entries(); 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 10] Local variable table: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:]表:[pc:0,pc:10] 本地:此索引:0 类型:dalvik.system.DexFile

// Method descriptor #30 ()V // Stack: 3, Locals: 1 protected void finalize() throws java.io.IOException; // 方法描述符 #30 ()V // Stack: 3, Locals: 1 protected void finalize() throws java.io.IOException; 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line: 11] Local variable table: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile 0 new java.lang.RuntimeException [2] 3 dup 4 ldc [3] 6 invokespecial java.lang.RuntimeException(java.lang.String) [4] 9 athrow Line numbers: [pc: 0, line:表:[pc:0,pc:10] 本地:此索引:0 类型:dalvik.system.DexFile

// Method descriptor #42 (Ljava/lang/String;)Z public static native boolean isDexOptNeeded(java.lang.String arg0) throws java.io.FileNotFoundException, java.io.IOException; // Method descriptor #42 (Ljava/lang/String;)Z public static native boolean isDexOptNeeded(java.lang.String arg0) throws java.io.FileNotFoundException, java.io.IOException; } }

Don't call thread.run() but thread.start() .不要调用thread.run()而是调用thread.start()

run() calls the function run() on the same thread. run() run() start() will call it in a new thread. start()将在新线程中调用它。 currently the GUI thread is blocked because it performs action that should be done in background thread.当前 GUI 线程被阻塞,因为它执行应该在后台线程中完成的操作。

You can either call the thread by:您可以通过以下方式调用线程:

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

Or make your loginThread extend Thread instead of implementing Runnable.或者让你的 loginThread扩展Thread 而不是实现 Runnable。

You retrieve the InputStream from your thread right away, it's more than likely that it still is null, or has its initial value, didn't get the http response yet.您立即从线程中检索InputStream ,它很可能仍然是 null,或者具有其初始值,但尚未得到 http 响应。 This way your progress dialog is shown for a few milliseconds at most.这样,您的进度对话框最多显示几毫秒。

End your loginRequest after calling thread.start() , and from within your thread's run method communicate if you got the result or any exception towards the UI thread using a Handler .在调用thread.start()后结束您的loginRequest ,如果您使用Handler获得结果或 UI 线程的任何异常,则从线程的run方法中进行通信。 In the handler's handleMessage check for the outcome of the loginRequest (the result input stream would be better to be passed to the handler directly, than to retrieve it from the thread), and remove your progress dialog.在处理程序的handleMessage检查loginRequest的结果(结果输入 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 最好直接传递给处理程序,而不是从线程中检索它),并删除您的进度对话框。

Update Here is a sample how to achieve what you want using threads:更新这里是一个示例如何使用线程实现您想要的:
In the code below your main activity is referenced as LoginActivity (since I don't know the name of your class), so you should change it to whatever the name of the java activity class is, where you have the loginRequest method.在下面的代码中,您的主要活动被引用为LoginActivity (因为我不知道您的类的名称),因此您应该将其更改为 java 活动 class 的名称,其中您有loginRequest方法。

Remove the showProgressDialog() method from your main activity, and instead put:从您的主要活动中删除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();
}

Note, that the loginRequest method has changed!请注意, loginRequest方法已更改!
So did your LoginThread class:您的LoginThread class 也是如此:

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);
        }
    }
}

Now your LoginThread instance has a new member of type Handler .现在你的LoginThread实例有一个Handler类型的新成员。 It is initialized via a constructor parameter (as the others), so you can have it declared final .它通过构造函数参数(与其他参数一样)进行初始化,因此您可以将其声明为final
This handler will notify your UI thread (main activity), if it has got the input stream from the http response, or if it failed for some reason.如果它从 http 响应中获得输入 stream,或者由于某种原因失败,此处理程序将通知您的 UI 线程(主要活动)。
Complete the Handler implementation, to get it do what you need.完成Handler实现,让它做你需要的。

I hope the code cleared up a bit this approach.我希望代码澄清了这种方法。

unless and until you call the show method of the progress Dialog it will not show up.除非您调用进度对话框的 show 方法,否则它不会显示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM