使用处理程序结束进度对话

时间:2011-08-11 18:41:40

标签: android handler progressdialog

我需要知道如何在完成http帖子后解除我的进度对话。

这是我到目前为止所做的:

httppost.setEntity(ME);

 HttpResponse responsePOST = client.execute(httppost);  
 HttpEntity resEntity = responsePOST.getEntity(); 

 InputStream inputstream = resEntity.getContent();
 BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));
 StringBuilder stringbuilder = new StringBuilder();
 String currentline = null; 

 while ((currentline = buffered.readLine()) != null)
    { 
     stringbuilder.append(currentline + "\n"); 
     String result = stringbuilder.toString(); 
     Log.v("HTTP UPLOAD REQUEST",result); 
     inputstream.close();  
     handler.sendMessage(handler.obtainMessage());}
      }
     catch (Exception e) {
     e.printStackTrace();
     Toast.makeText(share.this, "Upload failed", Toast.LENGTH_SHORT).show();
    }
    }

    });

    background.start();
    }

    Handler handler = new Handler (){
    public void HandleMessage (Message msg){
    dialog.dismiss();
    }
    };

它不起作用,我想在发送消息处理程序中放置什么来告诉处理程序解雇?提前致谢

修正之后,Janail给了我这些错误:

08-15 10:08:20.020: ERROR/AndroidRuntime(25690): FATAL EXCEPTION: Thread-10
08-15 10:08:20.020: ERROR/AndroidRuntime(25690): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.os.Handler.<init>(Handler.java:121)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.widget.Toast.<init>(Toast.java:68)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at android.widget.Toast.makeText(Toast.java:231)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at com.apapa.vrsixty.share$2$2.run(share.java:130)
08-15 10:08:20.020: ERROR/AndroidRuntime(25690):     at java.lang.Thread.run(Thread.java:1027)
08-15 10:08:20.781: ERROR/WindowManager(25690): Activity com.apapa.vrsixty.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054d2f8 that was originally added here
08-15 10:08:20.781: ERROR/WindowManager(25690): android.view.WindowLeaked: Activity com.apapa.vrsixty.share has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054d2f8 that was originally added here
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.ViewRoot.<init>(ViewRoot.java:277)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.Window$LocalWindowManager.addView(Window.java:433)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.Dialog.show(Dialog.java:265)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ProgressDialog.show(ProgressDialog.java:107)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ProgressDialog.show(ProgressDialog.java:90)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.apapa.vrsixty.share$2.onClick(share.java:91)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.View.performClick(View.java:2532)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.view.View$PerformClick.run(View.java:9293)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Handler.handleCallback(Handler.java:587)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.os.Looper.loop(Looper.java:143)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at android.app.ActivityThread.main(ActivityThread.java:4263)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at java.lang.reflect.Method.invokeNative(Native Method)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at java.lang.reflect.Method.invoke(Method.java:507)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-15 10:08:20.781: ERROR/WindowManager(25690):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

试试这个

HttpResponse responsePOST = client.execute(httppost);  
 HttpEntity resEntity = responsePOST.getEntity(); 

 InputStream inputstream = resEntity.getContent();
 BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));
 StringBuilder stringbuilder = new StringBuilder();
 String currentline = null; 

 while ((currentline = buffered.readLine()) != null)
    { 
     stringbuilder.append(currentline + "\n"); 
     String result = stringbuilder.toString(); 
     Log.v("HTTP UPLOAD REQUEST",result); 
     inputstream.close();  
     //handler.sendMessage(handler.obtainMessage());}
      }
     catch (Exception e) {
     e.printStackTrace();
     Toast.makeText(share.this, "Upload failed", Toast.LENGTH_SHORT).show();
    }
     Message msg = Message.obtainMessage();
     msg.arg1 = 1;
     handler.sendMessage(msg);

    }

    });

    background.start();
    }

    private final Handler handler = new Handler (){
    public void HandleMessage (Message msg){
         if(msg.arg1 == 1)
               dialog.dismiss();
    }
    };