Android 进度条不显示
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,超级位) public Final class dalvik.system.DexFile {
// 方法描述符 #8 (Ljava/io/File;)V // 堆栈:3,局部变量:2 public DexFile(java.io.File file) 抛出 java.io.IOException; 0 aload_0 [这个] 1 调用特殊 java.lang.Object() [1] 4 新的java.lang.RuntimeException [2] 7 重复 8 最不发达国家 [3] 10 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 13 投掷 行号: [pc:0,行:4] 局部变量表: [pc: 0, pc: 14] 本地:此索引:0 类型:dalvik.system.DexFile [pc: 0, pc: 14] local: 文件索引: 1 type: java.io.File
// 方法描述符 #18 (Ljava/lang/String;)V // 堆栈:3,局部变量:2 public DexFile(java.lang.String fileName) 抛出 java.io.IOException; 0 aload_0 [这个] 1 调用特殊 java.lang.Object() [1] 4 新的java.lang.RuntimeException [2] 7 重复 8 最不发达国家 [3] 10 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 13 投掷 行号: [pc:0,行:5] 局部变量表: [pc: 0, pc: 14] 本地:此索引:0 类型:dalvik.system.DexFile [pc: 0, pc: 14] local: 文件名索引: 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 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [pc:0,行:6] 局部变量表: [pc: 0, pc: 10] 本地:sourcePathName 索引:0 类型:java.lang.String [pc: 0, pc: 10] 本地:outputPathName 索引:1 类型:java.lang.String [pc: 0, pc: 10] local: flags index: 2 type: int
// 方法描述符 #28 ()Ljava/lang/String; // 堆栈:3,局部变量:1 公共 java.lang.String getName(); 0 新的 java.lang.RuntimeException [2] 3 重复 4 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [pc:0,行:7] 局部变量表: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile
// 方法描述符 #30 ()V // 堆栈:3,局部变量:1 public void close() 抛出 java.io.IOException; 0 新的 java.lang.RuntimeException [2] 3 重复 4 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [pc:0,行:8] 局部变量表: [pc: 0, pc: 10] local: this index: 0 type: 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 加载器); 0 新的 java.lang.RuntimeException [2] 3 重复 4 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [pc:0,行:9] 局部变量表: [pc: 0, pc: 10] 本地:此索引:0 类型:dalvik.system.DexFile [pc: 0, pc: 10] 本地:名称索引:1 类型:java.lang.String [pc: 0, pc: 10] local: loader 索引: 2 type: java.lang.ClassLoader
// 方法描述符 #37 ()Ljava/util/Enumeration; // 签名: ()Ljava/util/Enumeration; // 堆栈:3,局部变量:1 公共java.util.Enumeration条目(); 0 新的 java.lang.RuntimeException [2] 3 重复 4 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [pc:0,行:10] 局部变量表: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile
// 方法描述符 #30 ()V // 堆栈:3,局部变量:1 protected void Finalize() 抛出 java.io.IOException; 0 新的 java.lang.RuntimeException [2] 3 重复 4 最不发达国家 [3] 6 调用特殊 java.lang.RuntimeException(java.lang.String) [4] 9 投掷 行号: [PC:0,行:11] 局部变量表: [pc: 0, pc: 10] local: this index: 0 type: dalvik.system.DexFile
// 方法描述符 #42 (Ljava/lang/String;)Z public static native boolean isDexOptNeeded(java.lang.String arg0) 抛出 java.io.FileNotFoundException, java.io.IOException; }
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要调用
thread.run()
,而是调用thread.start()
。run()
在同一线程上调用函数run()
。start()
将在新线程中调用它。目前,GUI 线程被阻塞,因为它执行应在后台线程中完成的操作。您可以通过以下方式调用线程:
或者使您的loginThread扩展线程而不是实现Runnable。
Don't call
thread.run()
butthread.start()
.run()
calls the functionrun()
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:
Or make your loginThread extend Thread instead of implementing Runnable.
您立即从线程中检索
InputStream
,它很可能仍然为 null,或者具有初始值,但尚未获得 http 响应。这样您的进度对话框最多会显示几毫秒。调用
thread.start()
后结束您的loginRequest
,并在线程的run
方法中进行通信(如果您收到结果或任何异常)使用Handler
的 UI 线程。在处理程序的handleMessage
中检查loginRequest
的结果(结果输入流最好直接传递给处理程序,而不是从线程检索它),并删除进度对话框。更新
以下是如何使用线程实现您想要的目标的示例:
在下面的代码中,您的主要活动被引用为
LoginActivity
(因为我不知道您的类的名称),因此您应该将其更改为 java 活动类的名称,您在其中有loginRequest
方法。从主活动中删除
showProgressDialog()
方法,并改为:注意,
loginRequest
方法已更改!您的
LoginThread
类也是如此:现在您的
LoginThread
实例有了一个Handler
类型的新成员。它是通过构造函数参数初始化的(与其他参数一样),因此您可以将其声明为final
。如果该处理程序已从 http 响应获取输入流,或者由于某种原因失败,则该处理程序将通知您的 UI 线程(主活动)。
完成
Handler
实现,让它执行您需要的操作。我希望代码能澄清一点这种方法。
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 callingthread.start()
, and from within your thread'srun
method communicate if you got the result or any exception towards the UI thread using aHandler
. In the handler'shandleMessage
check for the outcome of theloginRequest
(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 theloginRequest
method.Remove the
showProgressDialog()
method from your main activity, and instead put:Note, that the
loginRequest
method has changed!So did your
LoginThread
class:Now your
LoginThread
instance has a new member of typeHandler
. It is initialized via a constructor parameter (as the others), so you can have it declaredfinal
.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.
除非您调用进度对话框的 show 方法,否则它不会显示。
unless and until you call the show method of the progress Dialog it will not show up.