简体   繁体   中英

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 {

// Method descriptor #8 (Ljava/io/File;)V // Stack: 3, Locals: 2 public DexFile(java.io.File file) 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: 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

// Method descriptor #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

// Method descriptor #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; 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

// Method descriptor #28 ()Ljava/lang/String; // 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

// Method descriptor #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

// Method descriptor #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); 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

// Method descriptor #37 ()Ljava/util/Enumeration; // Signature: ()Ljava/util/Enumeration; // 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

// Method descriptor #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

// 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() .

run() calls the function run() on the same thread. start() will call it in a new thread. currently the GUI thread is blocked because it performs action that should be done in background thread.

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.

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. 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 . 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.

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.

Remove the showProgressDialog() method from your main activity, and instead put:

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!
So did your 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 . It is initialized via a constructor parameter (as the others), so you can have it declared 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.
Complete the Handler implementation, to get it do what you need.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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