调用removeDialog后,进度对话框不会被忽略

时间:2012-09-28 07:18:05

标签: android progressdialog

我正在使用进度对话框来显示活动的进度。它开始运行,但即使在调用removeDialog()之后它也没有消失。这是代码:

public class MainActivity extends Activity {
    static final int PROGRESS_DIALOG = 0;
    ProgressDialog progressDialog = null;
    ProgressBar progressBar;
    MagickImage img = null;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        // InputStream stream = getResources().openRawResource(R.raw.app_notes);


        try {
            img = new MagickImage(new ImageInfo("/mnt/sdcard/image_background.jpg"));

//          setContentView(R.layout.main);
            Spinner s = (Spinner) findViewById(R.id.spinner);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);


            ArrayAdapter adapter = ArrayAdapter.createFromResource(MainActivity.this,R.array.effects, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);


            s.setOnItemSelectedListener(new OnItemSelectedListener() {

                public void onItemSelected(AdapterView<?> parent, View view,
                       int pos, long id) {
                    ProgressThread t = new ProgressThread(img, pos);
                    t.run();
                 //   progressDialog.dismiss();;

                }

                public void onNothingSelected(AdapterView<?> parent) {
                    // D       o nothing.
                }
            });

            ImageView iv = (ImageView) findViewById(R.id.imageView);
            iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            iv.setAdjustViewBounds(true);
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inSampleSize=8; //try to decrease decoded image
            options.inPurgeable=true; //if necessary purge pixels into disk
            options.inScaled=true; //scale down image to actual device density
            iv.setImageBitmap(MagickBitmap.ToBitmap(img));
        } catch (MagickApiException e) {
            e.printStackTrace();
        } catch (MagickException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /*
         * TextView tv = new TextView(this);
         * tv.setText(DisplayImageMetaData.displayMagickImage(img));
         * setContentView(tv);
         */
    }

    protected Dialog onCreateDialog(int id, Bundle bundle) {
        switch (id) {
        case PROGRESS_DIALOG:
            /*
             * ProgressDialog dialog =
             * ProgressDialog.show(AndroidMagickActivity.this, "",
             * "Loading. Please wait...", true);
             */
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setMessage("Loading...");
            progressDialog.show(MainActivity.this,"", "Loading... Please Wait", true);
            return progressDialog;
            // return dialog;
        default:
            return null;
        }
    }


    /** Nested class that performs progress calculations (counting) */
    private class ProgressThread extends Thread {

        MagickImage img;
        int pos;

        ProgressThread(MagickImage image, int pos) {
            this.img = image;
            this.pos = pos;
        }

        public void run() {

            int effect = 0;
            MainActivity.this.showDialog(PROGRESS_DIALOG);
            new BackgroundAsyncTask().execute();
            switch (pos) {
            case 1:
                effect = NoiseType.UndefinedNoise;
                break;
            case 2:
                effect = NoiseType.UniformNoise;
                break;
            case 3:
                effect = NoiseType.GaussianNoise;
                break;
            case 4:
                effect = NoiseType.MultiplicativeGaussianNoise;
                break;
            case 5:
                effect = NoiseType.ImpulseNoise;
                break;
            case 6:
                effect = NoiseType.LaplacianNoise;
                break;
            case 7:
                effect = NoiseType.PoissonNoise;
                break;
            }
            Bitmap bitmap = null;
            try {
                ImageView iv = (ImageView) findViewById(R.id.imageView);

                MagickImage image = null;
                if (pos < 8)
                    image = img.addNoiseImage(effect);
                else if (pos == 9)
                    image = img.blurImage(5, 1);
                else if (pos == 10)
                    image = img.charcoalImage(5, 1);
                else if (pos == 11)
                    image = img.edgeImage(0);
                if (image != null) {
                      iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                      iv.setAdjustViewBounds(true);
                      BitmapFactory.Options options=new BitmapFactory.Options();
                      options.inSampleSize=8; //try to decrease decoded image
                      options.inPurgeable=true; //if necessary purge pixels into disk
                      options.inScaled=true; //scale down image to actual device density
                      bitmap = MagickBitmap.ToBitmap(image);
                      iv.setImageBitmap(bitmap);
                }

                MainActivity.this.removeDialog(PROGRESS_DIALOG);
            } catch (MagickException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally
            {
                MainActivity.this.removeDialog(PROGRESS_DIALOG);
            }
        }
    }    
}

我不知道我犯了哪个错误。请帮帮我。谢谢。

4 个答案:

答案 0 :(得分:1)

尝试:

ProgressDialog.dismiss();

答案 1 :(得分:0)

您可以使用以下方式执行此操作:

progressDialog.dismiss();

答案 2 :(得分:0)

您可以尝试保留对话框的引用,然后调用progressDialog.dismiss()。我不确定你为什么不工作。

答案 3 :(得分:0)

if(ProgressDialog.isShowing()){

ProgressDialog.dismiss();

}