活动已泄露窗口com.android.internal.policy.impl.PhoneWindow$DecorView@46029dd0

时间:2013-12-24 06:21:24

标签: java android android-dialog android-dialogfragment

我正在开发一种反馈类型的应用程序,当我点击“submitnow”按钮时,我收到以下错误

活动已泄露窗口

com.android.internal.policy.impl.PhoneWindow$DecorView@46029dd0

以下是我的代码,请帮帮我。

public class SignOut_Activity extends SherlockActivity implements
        OnClickListener {

    Button btnSubmitNow, btnSubmitLater;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.signout);

        ((TextView) findViewById(R.id.tvSubTitle))
                .setText(StoresListAdapter.StoreName);

        btnSubmitNow = (Button) findViewById(R.id.btnSubmitNow);
        btnSubmitLater = (Button) findViewById(R.id.btnSubmitLater);

        btnSubmitNow.setOnClickListener(this);
        btnSubmitLater.setOnClickListener(this);

        progressDialog = new ProgressDialog(SignOut_Activity.this);
        progressDialog.setMessage("Uploading data, please wait...");

    }

    @Override
    public boolean onOptionsItemSelected(
            com.actionbarsherlock.view.MenuItem item) {

        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; finish activity to go home
            finish();
            break;

        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onResume() {
        super.onResume();
        // Set title
        getSupportActionBar().setTitle("Sign Out");

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.btnSubmitNow:
            // Submit now

            // Sample upload image
            UploadImage.uploadImage("testimage");

            new AsyncTask<Void, Void, Void>() {

                // called before execution // main/UI thread
                protected void onPreExecute() {
                    progressDialog.show();
                };

                @Override
                protected Void doInBackground(Void... params) {
                    // Submit the store data
                    StoreData.postData(StoreList_Activity.storesList
                            .get(StoresListAdapter.Position));
                    return null;
                }

                // on store data uploaded // main/UI thread
                protected void onPostExecute(Void result) {
                    progressDialog.dismiss();
                    setSignOut();
                    StoreList_Activity.storesList
                            .get(StoresListAdapter.Position).isSubmitted = true;
                    SignOut_Activity.this.finish();
                };

            }.execute();

            break;
        case R.id.btnSubmitLater:
            // Submit later
            setSignOut();
            StoreList_Activity.storesList.get(StoresListAdapter.Position).isSubmitLater = true;
            VisitOps_Activity.isSubmitLater = true;
            SignOut_Activity.this.finish();

            break;

        default:
            break;
        }

    }

    @SuppressLint("SimpleDateFormat")
    private void setSignOut() {

        VisitOp visitOp10 = new VisitOp();
        visitOp10.setName("Sign Out");
        visitOp10.setStatus("");
        SampleData.visitOpsList.add(visitOp10);

        if (VisitOps_Activity.VisitOps.SignOut == null)
            VisitOps_Activity.VisitOps.SignOut = new SignOut();

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String currentDateandTime = sdf.format(new Date());

        VisitOps_Activity.VisitOps.SignOut.SignOutTime = "Out: "
                + currentDateandTime;

    }
}

5 个答案:

答案 0 :(得分:11)

泄漏是因为你在销毁之后仍然保留活动的参考所以使用

if(progressDialog !=null)
{
    progressDialog = null;
}
progressDialog = new ProgressDialog(this.getApplicationContext());
        progressDialog.setMessage("Uploading data, please wait...");

使用它会有所帮助

@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.btnSubmitNow:
        // Submit now

        // Sample upload image
        UploadImage.uploadImage("testimage");

        new AsyncTask<Void, Void, Void>() {

            // called before execution // main/UI thread
            protected void onPreExecute() {


             progressDialog = new ProgressDialog(SignOut_Activity.this);
             progressDialog.setMessage("Uploading data, please wait...");  

            progressDialog.show();
            };

            @Override
            protected Void doInBackground(Void... params) {
                // Submit the store data
                StoreData.postData(StoreList_Activity.storesList
                        .get(StoresListAdapter.Position));
                return null;
            }

            // on store data uploaded // main/UI thread
            protected void onPostExecute(Void result) {
                progressDialog.dismiss();
                setSignOut();
                StoreList_Activity.storesList
                        .get(StoresListAdapter.Position).isSubmitted = true;
                SignOut_Activity.this.finish();
            };

        }.execute();

答案 1 :(得分:4)

  

为什么会出现此错误??

当您保留对未使用活动的引用时会发生此错误

  

解决方案

使用后删除进度条,对话框等参考。

解雇他们或使他们等于null

你可以在不再需要时接近它

推荐将其放入onStop

 @Override
    protected void onStop() {
  super.onStop();
        if(_dialog.isShowing()){
            _dialog.dismiss();
        }
        if(_dialog != null){
            _dialog = null;
        }
}

答案 2 :(得分:3)

使用后忽略dialog,它不会让您的应用程序崩溃。

dialog.dismiss();

答案 3 :(得分:2)

使用该代码progressDialog.dismiss();

答案 4 :(得分:1)

确保在对话框使用后以及任何下一个后台进程启动之前解除()对话框。