首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProgressDialog错误

ProgressDialog错误
EN

Stack Overflow用户
提问于 2011-01-05 09:58:41
回答 3查看 762关注 0票数 0

我的代码如下:

代码语言:javascript
复制
           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;
    }




}

不显示任何对话框!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-01-05 16:51:40

不能直接从后台线程对UI执行更新。

如果您一定要使用Thread,那么可以使用onCreateDialog来准备新的对话框,让它正常工作。然后调用showDialog(int)dismissDialog(int)来显示/隐藏对话框:

代码语言:javascript
复制
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();
        }
    }
});
票数 1
EN

Stack Overflow用户

发布于 2011-01-05 10:03:02

您正试图在UI线程之外调用与UI相关的API函数-在本例中,假定为pd.dismiss()。任何与UI有关的事情都需要在主线程上运行--一个简单的实现方法是通过Activity.runOnUiThread()

票数 1
EN

Stack Overflow用户

发布于 2011-01-05 13:39:00

我同意@EboMike的观点,无论你尝试了什么,都可能是错误的。

但我的建议是,使用AsyncTask是一种更干净的方式来做你想要做的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4600200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档