2011-01-05 46 views
0

海蘭我的代碼:ProgressDialog錯誤

  package com.android.skiptvad; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.android.skiptvad.*; 

public class Login extends Activity { 
    private static final int DIALOG_LOADING = 0; 
    /** Called when the activity is first created. */ 
    TextView tvuser; 
    String sessionid; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 


     tvuser = (TextView) findViewById(R.id.tvuser); 
     TextView tvpw = (TextView) findViewById(R.id.tvpw); 
     final EditText etuser = (EditText) findViewById(R.id.etuser); 
     final EditText etpw = (EditText) findViewById(R.id.etpw); 
     Button btlogin = (Button)findViewById(R.id.btlogin); 
     btlogin.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (etuser.getText() != null && etpw.getText()!= null) 
       { 
        showDialog(DIALOG_LOADING); 
        try 
        { 
        //download(etuser.getText().toString(), md5(etpw.getText().toString())); 
        HttpClient client = new DefaultHttpClient(); 
        String postURL = "http://surfkid.redio.de/login"; 
        HttpPost post = new HttpPost(postURL); 
         List<NameValuePair> params = new ArrayList<NameValuePair>(); 
         params.add(new BasicNameValuePair("username", etuser.getText().toString())); 
         params.add(new BasicNameValuePair("password", md5(etpw.getText().toString()))); 
         UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
         post.setEntity(ent); 

         HttpResponse responsePOST = client.execute(post); 
         HttpEntity resEntity = responsePOST.getEntity(); 
         final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity)); 
         JSONObject menuObject = jObject.getJSONObject("responseData"); 

         if (jObject.getInt("responseStatus")== 200 && jObject.get("responseDetails")!= null) 
         { 
          sessionid = menuObject.getString("session_id"); 


         } 

         else 
         { 

          if (jObject.getInt("responseStatus")== 500) 
          { 
           throw new Exception("Server Error"); 
          } 
          else if (jObject.getInt("responseStatus")== 400) 
          { 
           throw new Exception("Wrong User/Password"); 
          } 
          else 
          { 
           throw new Exception(); 
          } 
         } 

        } 
        catch (Exception e) 
        { 
         Log.d("error", "error"); 
        } 
        finally{ 
         dismissDialog(DIALOG_LOADING); 
        } 
       } 

      } 
     }); 


    } 

    public void download (final String user, final String pw) 
    { 



    } 
    private String md5(String in) { 

     MessageDigest digest; 

     try { 

      digest = MessageDigest.getInstance("MD5"); 

      digest.reset();   

      digest.update(in.getBytes()); 

      byte[] a = digest.digest(); 

      int len = a.length; 

      StringBuilder sb = new StringBuilder(len << 1); 

      for (int i = 0; i < len; i++) { 

       sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16)); 

       sb.append(Character.forDigit(a[i] & 0x0f, 16)); 

      } 

      return sb.toString(); 

     } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } 

     return null; 

    } 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     Dialog dialog = null; 
     switch (id) { 
     case DIALOG_LOADING: 
      dialog = new ProgressDialog(this); 
      ((ProgressDialog) dialog).setMessage("Loading, please wait..."); 
      break; 
     } 
     return dialog; 
    } 




} 

沒有顯示對話框!

回答

1

您無法直接從後臺線程對UI進行更新。

如果您絕對必須使用Thread,那麼您可以使用onCreateDialog來準備新的對話框。然後調用showDialog(int)dismissDialog(int)顯示/隱藏對話框:

private static final int DIALOG_LOADING = 0; 

... 
@Override 
protected Dialog onCreateDialog(int id) { 
    Dialog dialog; 
    switch (id) { 
    case DIALOG_LOADING: 
     dialog = new ProgressDialog(this); 
     ((ProgressDialog) dialog).setMessage("Loading, please wait..."); 
     break; 
    } 
    return dialog; 
} 

btlogin.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (etuser.getText() != null && etpw.getText()!= null) 
     { 
      showDialog(DIALOG_LOADING); 
      Thread t = new Thread() { 
       public void run(){ 
        try{ 
         download(etuser.getText().toString(), md5(etpw.getText().toString())); 
        } catch(Exception e) { 
         Log.e("TAG","Exception caught in thread:"+e.toString()); 
         e.printStackTrace(); 
        } finally { 
         try{ 
          dismissDialog(DIALOG_LOADING); 
         } catch (IllegalArgumentException e) { 
          Log.w("TAG","Dialog does not exist"); 
         } 
        } 
        //finish(); 
       } 
      }; 
      t.start(); 
     } 
    } 
}); 
+0

我試過你的代碼,但解僱不工作,對話框始終顯示 – user547995 2011-01-06 12:52:42

+0

它是否在日誌中引發異常? – 2011-01-06 13:07:59

+0

我已經更新了代碼以添加更好的異常處理,也許您可​​以重新測試? – 2011-01-06 13:18:32

1

您正試圖在UI線程之外調用與UI相關的API函數 - 在這種情況下,大概是pd.dismiss()。任何與UI有關的事情都需要在主線程上運行 - 一個簡單的方法可以通過Activity.runOnUiThread()來完成。

+0

也是不行我發佈 – user547995 2011-01-05 02:12:52

+0

好之前測試過它,發佈您的代碼,並告訴我們你嘗試過什麼。 – EboMike 2011-01-05 09:01:22

+0

是的,我發佈了它 – user547995 2011-01-06 13:34:40

1

我同意@EboMike,無論你嘗試過什麼都可能是錯誤的。

但我的建議是,你使用AsyncTask這是一個更乾淨的方式做你正在做的事情。