0

我正在使用进度对话框来显示活动的进度。它开始运行,但即使在调用 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

4 回答 4

1

尝试:

ProgressDialog.dismiss();
于 2012-09-28T07:20:26.250 回答
0

您可以使用:

progressDialog.dismiss();
于 2012-09-28T07:19:37.557 回答
0

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

于 2012-09-28T07:21:09.810 回答
0

如果(ProgressDialog.isShowing()){

ProgressDialog.dismiss();

}

于 2012-09-28T08:03:55.537 回答