安卓:活动已泄露,由于进度对话框? [英] Android: activity has leaked window due to progress dialog?

查看:109
本文介绍了安卓:活动已泄露,由于进度对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发展我的第一个Android应用程序,我想显示进度对话框,而在我的应用程序登录按钮,用户点击。所以我在集成应用的AsyncTask,就像登录注销成功完成,但问题是,在成功登录像LoginActivity这给我的错误已经泄露,由于进度对话框窗口中的所有操作。如何解雇进度对话框,并更新UI。

I am developing my first Android App and I want to display progress dialog while user click on login button in my apps. so I integrated asynctask in apps, all operation like login logout successfully done but problem is that after successfully login this giving me error like LoginActivity has leaked window due to progress dialog. how to dismiss progress dialog and update the UI.

请参考以下code和告诉我一些修改

please refer following code and tell me some changes

以下是LoginActivity

following is the LoginActivity

public class LoginActivity extends SherlockActivity {
.................
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    sessionmngr = new SessionManager(this);
    //check the user login or not
    if (sessionmngr.isLoggedIn()) {
        Intent checkLoginIntnt = new Intent(this,ProjectFragActivity.class);
        startActivity(checkLoginIntnt);
    }
    setContentView(R.layout.activity_login);
    ........
}
// onclick listener when click on login activity

public void LoginToBookingScape(View v) throws JSONException {

    username = edtTxtUserName.getText().toString();
    userpsw = edtTxtUserPsw.getText().toString();

    if ((username.trim().length() > 0)&&(userpsw.trim().length() > 0)) {

        JsonWebService jsonWebs = new JsonWebService();
        jsonWebs.execute(loginUrl);

    }else {
        ............
    }   
}

以下是内部类中LoginActivity扩展的AsyncTask

Following is the Inner class to extend AsyncTask in LoginActivity

private class JsonWebService extends AsyncTask<String,Void,String> {

    private ProgressDialog dialogLogin;
    @Override
    protected String doInBackground(String... url) {

                httpPost.setEntity(new UrlEncodedFormEntity(params));
                ....
                inStream = httpEntity.getContent();
                .........
                return jsonResp;
    }

    @Override
    protected void onPostExecute(String jsonData) {
        //get string data from doinBackground
        try {
            JSONObject jsonObj = new JSONObject(jsonData);
            String key_login = jsonObj.getString(KEY_LOGIN);

            if (key_login.equalsIgnoreCase("0")) {
                .............

            }else {
                ....
                sessionmngr = new SessionManager(getApplicationContext());

                sessionmngr.createLoginSession(id,jsonObj.getString(KEY_UNAME), 
                        jsonObj.getString(KEY_UEMAIL));

                dialogLogin = ProgressDialog.show(LoginActivity.this, "Bookingscape", 
                        "Please Wait",true);
                dialogLogin.setIcon(R.drawable.icon);
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                Intent inteProj = new Intent(getApplicationContext(),           

                    ProjectFragActivity.class);
                startActivity(inteProj);
                finish();   
            }
        ........
    }
    @Override
    protected void onCancelled() {
        dialogLogin.dismiss();
        dialogLogin = null;
        super.onCancelled();
    }
}

}

我想在这里问一个问题

以上是code优化和可重复使用的。

Is above code optimize and reusable.

在此先感谢

推荐答案

是要转移到新的活动,而不驳回进度对话的问题。这将导致泄漏的窗口误差

The problem is you are moving to new activity without dismissing the progress dialogue . this will cause leaked window error

我想你必须移动 dialogLogin.dismiss(); 表格 onCancelled()阻止对<$ C $在code C> onPostExecute 块

I think you must move dialogLogin.dismiss(); form onCancelled()block to onPostExecute block in your code

您必​​须要去到另一个活动之前做到这一点。即前

you must do this before you are going to another activity . ie before

Intent inteProj = new Intent(getApplicationContext(),ProjectFragActivity.class);
startActivity(inteProj);

这行code的。我认为这将解决您的问题。

this line of code .I think this will solve your issue

一个疑问:哪里是你的preExecute?通常我在该块显示进度code和解雇,在onPostExecute

one doubt : where is your onPreExecute ?? Usually i display progress code in that block and dismiss that in onPostExecute

通常情况下,流动是这样的上preExecute - &GT; doInBackground ---&GT; onPostExecute

Usually the flow is like this onPreExecute-->doInBackground --->onPostExecute

编辑:

在preExecute:初始化你的UI组件(例如:Dialoges)。在你的情况 ProgressDialog 出现了

onPreExecute: Initialize your UI components (eg: Dialoges) .In your case ProgressDialog showed up

doInBackground:在上preExecute 块控制进入到这个块,这将在。这里 ProgressDialog 继续它的工作

doInBackground : After onPreExecute block control goes to this block this will .Here the ProgressDialog continues it's work

onPostExecute:控制来这里所有的后台操作之后。在这里,你可以辞退你的 ProgressDialog 和GOTO新的活动。

onPostExecute : control come here after all background action .Here you can dismiss your ProgressDialog and goto your new activity.

这篇关于安卓:活动已泄露,由于进度对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆