Android的进度条显示不出来进度条、不出来、Android

2023-09-06 05:22:35 作者:酷到沒朋友

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

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

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

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

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

//方法描述符#8(Ljava / IO /文件;)V  //堆栈:3,当地人:2  公共DexFile(java.io.File中的文件)产生java.io.IOException;     0 aload_0 [这]     1 invokespecial java.lang.Object中()[1]     4个新的了java.lang.RuntimeException [2]     7 DUP     8 LDC [3]    10 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]    13 athrow      行号:        [PC:0,行:4]      局部变量表:        [PC:0,邮编:14]地方:这个指数:0类型:dalvik.system.DexFile        [PC:0,邮编:14]地方:文件索引:1类型:java.io.File中的

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

//方法描述符#18(Ljava /朗/字符串;)V  //堆栈:3,当地人:2  公共DexFile(java.lang.String中文件名)产生java.io.IOException;     0 aload_0 [这]     1 invokespecial java.lang.Object中()[1]     4个新的了java.lang.RuntimeException [2]     7 DUP     8 LDC [3]    10 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]    13 athrow      行号:        [PC:0,行:5]      局部变量表:        [PC:0,邮编:14]地方:这个指数:0类型:dalvik.system.DexFile        [PC:0,邮编:14]地方:文件名索引:1类型:java.lang.String中

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

//方法描述符#22(Ljava /朗/字符串; Ljava /朗/字符串; I)Ldalvik /系统/ DexFile;  //堆栈:3,当地人:3  公共静态dalvik.system.DexFile loadDex(java.lang.String中sourcePathName,java.lang.String中outputPathName,INT标志)产生java.io.IOException;     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:6]      局部变量表:        [PC:0,邮编:10]地方:sourcePathName指数:0类型:java.lang.String中        [PC:0,邮编:10]地方:outputPathName指数:1类型:java.lang.String中        [PC:0,邮编:10]地方:国旗指数:2类型:INT

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

//方法描述符#28()Ljava /朗/字符串;  //堆栈:3,当地人:1  公共java.lang.String中的getName();     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:7]      局部变量表:        [PC:0,邮编:10]地方:这个指数:0类型:dalvik.system.DexFile

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

//方法描述符#30()V  //堆栈:3,当地人:1  公共无效的close()产生java.io.IOException异常;     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:8]      局部变量表:        [PC:0,邮编:10]地方:这个指数:0类型: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

//方法描述符#32(Ljava /朗/字符串; Ljava /朗/ ClassLoader的;)Ljava /朗/班;  //堆栈:3,当地人:3  公共java.lang.Class中的loadClass(java.lang.String中的名字,java.lang.ClassLoader的装载机);     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:9]      局部变量表:        [PC:0,邮编:10]地方:这个指数:0类型:dalvik.system.DexFile        [PC:0,邮编:10]地方:名称指数:1类型:java.lang.String中        [PC:0,邮编:10]地方:装载机指数:2类型:java.lang.ClassLoader中的

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

//方法描述符#37()Ljava / UTIL /枚举;  //签名:()Ljava / UTIL /枚举;  //堆栈:3,当地人:1  公共的java.util.Enumeration条目();     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:10]      局部变量表:        [PC:0,邮编:10]地方:这个指数:0类型:dalvik.system.DexFile

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

//方法描述符#30()V  //堆栈:3,当地人:1  保护无效的finalize()产生java.io.IOException;     0新了java.lang.RuntimeException [2]     3 DUP     4 LDC [3]     6 invokespecial了java.lang.RuntimeException(java.lang.String中)[4]     9 athrow      行号:        [PC:0,行:11]      局部变量表:        [PC:0,邮编:10]地方:这个指数:0类型: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

//方法描述符#42(Ljava /朗/字符串;)z  isDexOptNeeded(java.lang.String中为arg0)公共静态本地布尔抛出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; }

推荐答案

您检索的InputStream 从你的线程正确了,它更可能,它仍然为空,还是有它的初始值,没有得到HTTP响应呢。通过这种方式显示你的进度对话框几毫秒最多。

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.

结束你的 loginRequest 呼叫后 thread.start(),并从你的线程的运行办法沟通,如果你有结果或使用处理程序。在处理程序中的的handleMessage 检查的 loginRequest (结果输入流的结果会更好传递给直接处理程序,而不是从线程检索它),并删除进度对话框。

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.

更新下面是一个示例如何实现你想要使用线程的内容:在您的主要活动低于code作为 LoginActivity 引用(因为我不知道你的类的名称),所以你应该将其更改为任何名称java的活动类是,在这里你还可以使用 loginRequest 方法。

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.

从您的主要活动中删除 showProgressDialog()方法,而是把:

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

请注意,该 loginRequest 方法变了!所以做你的 LoginThread 类:

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

现在你的 LoginThread 实例有型的新成员处理程序。它是通过构造函数的参数(如其他)初始化,所以你可以把它声明最后 该处理器会通知你的UI线程(主要活动),如果它已经得到了来自HTTP响应输入流,或者如果它由于某种原因失败。 完成处理程序的实施,得到它做你的需要。

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.

我希望code清理了一下这种方法。

I hope the code cleared up a bit this approach.

 
精彩推荐
图片推荐