活动泄露信息+进度对话+ Android

时间:2016-02-10 07:00:01

标签: android-intent android-activity android-asynctask progressdialog android-context

在我的活动中,我没有使用任何asynctask。该活动的目的是将数据从api加载到sqlite数据库。因此我在我的活动的oncreate()上使用了LoadData()。 我想在加载数据时实现进度对话,这意味着加载信息的时间。我用过这个

 pd = new ProgressDialog(OpeningActivity.this, ProgressDialog.STYLE_SPINNER);
    pd.setIndeterminate(true);
    pd.show(OpeningActivity.this, AppConstants.WAITTAG, AppConstants.WAITDET);

完成progressDlg: 我活动中的其他案例很少。基于此我完成了对话活动并称之为新的活动意图

if (db.isTableExists(db3,EDU_PROVIDER_TABLE)){
                pd.dismiss();
                Intent a = new Intent(getApplicationContext(),PlaceChoiceActivity.class);//Default Activity
                a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //getApplicationContext().startActivity(a);
                (getApplicationContext()).startActivity(a);

                this.finish();
            }

我的logcat

ctivity.OpeningActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41dfc8f0 V.E..... R.....ID 0,0-480,243} that was originally added here
                                                                          android.view.WindowLeaked: Activity demo.kolorob.kolorobdemoversion.activity.OpeningActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41dfc8f0 V.E..... R.....ID 0,0-480,243} that was originally added here

我尝试使用getApplicationContext()替换OpeningActivity.this或者这个或者getActivity()也尝试传递抛出异常的上下文,因为无法强制转换android.app.application can not cast to android.app.activity

1 个答案:

答案 0 :(得分:1)

试试这个 您传递错误的上下文只是传递活动上下文。

class ShowProgress extends AsyncTask<Void, Void, Void>{

        ProgressDialog pdialog = new ProgressDialog(MainActivity.this);


        @Override
        protected void onPreExecute() {

            pdialog.setMessage("message");
            pdialog.setTitle("title");
            pdialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            if(pdialog != null && pdialog.isShowing()){
                pdialog.dismiss();
                 }

            insertNewData();
        }

    }