再次泄露了窗户

时间:2012-06-25 15:22:00

标签: android

06-25 15:03:02.596: E/WindowManager(29217):     at com.android.grad.PreviewActivity$1.onClick(PreviewActivity.java:36)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.view.View.performClick(View.java:3527)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.view.View$PerformClick.run(View.java:14234)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.os.Handler.handleCallback(Handler.java:605)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.os.Handler.dispatchMessage(Handler.java:92)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.os.Looper.loop(Looper.java:137)
    06-25 15:03:02.596: E/WindowManager(29217):     at android.app.ActivityThread.main(ActivityThread.java:4441)
    06-25 15:03:02.596: E/WindowManager(29217):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-25 15:03:02.596: E/WindowManager(29217):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-25 15:03:02.596: E/WindowManager(29217):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    06-25 15:03:02.596: E/WindowManager(29217):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    06-25 15:03:02.596: E/WindowManager(29217):     at dalvik.system.NativeStart.main(Native Method)
    06-25 15:03:04.186: I/Process(29217): Sending signal. PID: 29217 SIG: 9
    06-25 15:03:04.566: D/TextLayoutCache(29339): Using debug level: 0 - Debug Enabled: 0
    06-25 15:03:04.606: D/libEGL(29339): loaded /system/lib/egl/libGLES_android.so
    06-25 15:03:04.606: D/libEGL(29339): loaded /system/lib/egl/libEGL_adreno200.so
    06-25 15:03:04.636: D/libEGL(29339): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
    06-25 15:03:04.636: D/dalvikvm(29339): GC_CONCURRENT freed 117K, 3% free 9125K/9351K, paused 4ms+17ms
    06-25 15:03:04.636: D/libEGL(29339): loaded /system/lib/egl/libGLESv2_adreno200.so
    06-25 15:03:04.706: D/OpenGLRenderer(29339): Enabling debug mode 0
    06-25 15:03:07.326: D/OpenGLRenderer(29339): Flushing caches (mode 0)
    06-25 15:03:07.326: W/IInputConnectionWrapper(29339): showStatusIcon on inactive InputConnection
    06-25 15:03:07.366: D/OpenGLRenderer(29339): Flushing caches (mode 1)

我知道之前提到的这个错误,我看到了解决方案,但解决方案不能与我合作。这是我的代码。

错误出现在pd.show()。

@Override
protected void onPreExecute() {
    //network.loadData();
    file = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MyCameraApp" + File.separator + "project");
    createProgressDialog();
    pd.show();
}

@Override
protected Boolean doInBackground(String... params) {
    try {
        crop = new Crop(params[0]);
        crop.cropID();
        croppedID = crop.getId();
        publishProgress(2);
    } catch (IOException e) {
    }

    segmentNumbers();
    return true;
}

@Override
protected void onProgressUpdate(Integer... values) {
    pd.incrementProgressBy(values[0]);
}

@Override
protected void onPostExecute(Boolean result) {
    pd.dismiss();
    Toast.makeText(activity, "Processing Done", Toast.LENGTH_LONG).show();
}

private void createProgressDialog() {
    pd = new ProgressDialog(activity);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setTitle("Extract ID");
    pd.setMessage("Processing...");
    pd.setIcon(R.drawable.ic_launcher);
    pd.setProgress(0);
    pd.setCancelable(false);
}

onpreExecution方法: -           我正在显示我的对话框。 doInBackGround方法: -           我执行我用于分割图片的segmentNumber()。 onPostExecution方法: -           我正在驳回我的对话。

修改后的代码:

package com.OCR.ID;

import java.io.File;
import java.io.IOException;

import org.neuroph.contrib.imgrec.image.Image;
import org.neuroph.contrib.imgrec.image.ImageFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.Camera.R;

    /**
     * @author Mahmud
     * 
 */
public class Segement extends AsyncTask<String, Integer, Boolean> {

    int[][] croppedID;
    Crop crop;
    private Activity activity;
    private ProgressDialog pd;
    File file;
    private String ID;
    private Neuroph network;

    public Segement(Activity activity, ProgressDialog pd) throws IOException {
        this.activity = activity;
        this.pd = pd;
        //network = new Neuroph(this.activity);
    }

    @Override
    protected void onPreExecute() {
        //network.loadData();
        file = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "MyCameraApp" + File.separator + "project");
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            crop = new Crop(params[0]);
            crop.cropID();
            croppedID = crop.getId();
            publishProgress(2);
        } catch (IOException e) {
        }

        segmentNumbers();
        return true;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        pd.incrementProgressBy(values[0]);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
        Toast.makeText(activity, "Processing Done & your ID : " + ID,
                Toast.LENGTH_LONG + Toast.LENGTH_LONG).show();
    }


    /**
     * Segment cropped image into number
     */
    public void segmentNumbers() {
        int count = 0, endOfNumber = 0, startOfNumber = 0;
        int[][] number = null;

        while (crop.getxProjection()[endOfNumber] == 0
                && endOfNumber != crop.getxProjection().length - 1)
            endOfNumber++;

        while (endOfNumber != croppedID.length - 1) {
            endOfNumber = startOfNumber;

            while (crop.getxProjection()[endOfNumber] != 0
                    && endOfNumber != crop.getxProjection().length - 1)
                endOfNumber++;

            if (endOfNumber - startOfNumber < 10) {
                startOfNumber = endOfNumber;
                while (crop.getxProjection()[startOfNumber] == 0
                        && endOfNumber != crop.getxProjection().length - 1)
                    startOfNumber++;
                continue;
            }

            if (startOfNumber >= croppedID.length
                    || endOfNumber >= croppedID.length)
                break;

            number = new int[endOfNumber - startOfNumber - 1][croppedID[0].length];

            for (int i = 0; i < number.length; i++)
                for (int j = 0; j < number[i].length; j++)
                    number[i][j] = croppedID[i + startOfNumber][j];

            number = verticalProjectionForNumber(number);

            File numberFile = AndroidImage.save(activity,
                    AndroidImage.write2DIntoImage(number),
                    Integer.toString(count));
//          Image image = ImageFactory.getImage(numberFile.getAbsoluteFile());
//          ID += network.recognize(image);

            //
            // file.getPath() + File.separator + Integer.toString(count) +
            // ".PNG");
            publishProgress(7);

            startOfNumber = endOfNumber;
            while (crop.getxProjection()[startOfNumber] == 0
                    && endOfNumber != crop.getxProjection().length - 1)
                startOfNumber++;

            count++;
        }

        Log.d("ID", ID);
    }

    /**
     * Do vertical projection on each number
     */
    private int[][] verticalProjectionForNumber(int[][] number) {

        int[] projection = new int[number[0].length];

        for (int i = 0; i < (number[0].length * 0.90); i++) {
            for (int j = 0; j < number.length; j++) {
                projection[i] += number[j][i];
            }
        }

        int first = 0;
        while (projection[first] == 0)
            first++;

        int last = projection.length - 1;
        while (projection[last] == 0)
            last--;

        int[][] modifiedNumber = new int[number.length][last - first];
        for (int i = 0; i < modifiedNumber.length; i++)
            for (int j = 0; j < modifiedNumber[i].length; j++)
                modifiedNumber[i][j] = number[i][j + first];

        number = null;
        projection = null;
        System.gc();

        return modifiedNumber;
    }
}

我从这里打电话给任务: -

OnClickListener processOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            try {
                createProgressDialog();
                pd.show();
                new Segement(PreviewActivity.this, pd).execute(path);
            } catch (IOException e) {
            }
        }
    };

2 个答案:

答案 0 :(得分:3)

夏威夷上面的回答是正确的。

更具体地说,doInBackground方法与UI线程同步。因此,您永远不应该在此方法中更新您的视图/布局/任何与UI相关的内容。我建议您在show()执行之前AsyncTask对话。

答案 1 :(得分:0)

在执行任务之前,您应该show() Dialog AsyncTask

OnProgressUpdate中,您只能使用onPostExecuteUI方法更新createProgressDialog(); pd.show(); task.execute();

private void createProgressDialog() {
    pd = new ProgressDialog(YourMainActivity.this);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setTitle("Extract ID");
    pd.setMessage("Processing...");
    pd.setIcon(R.drawable.ic_launcher);
    pd.setProgress(0);
    pd.setCancelable(false);
}

将您的方法更改为

{{1}}