2012-06-25 77 views
1
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()用於segement圖片。 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) { 
      } 
     } 
    }; 
+0

除了我沒有看到pd的任何賦值之外,無法從代碼中發現太多內容。 – matt5784

+0

看到這篇文章http://blogactivity.wordpress.com/2011/09/01/proper-use-of-asynctask/ – slezica

+0

我們可以在'AsyncTask'中看到其餘的代碼嗎?如何分配實例?現在看起來它是空的。 – Devunwired

回答

0

您應該show()您在執行任務之前Dialog

AsyncTask中,您只能使用OnProgressUpdateonPostExecute方法更新您的UI

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); 
} 
+0

不知道我在哪裏可以打電話執行? –

+0

在你的Java代碼中,在你執行你的AsyncTask之前,你將顯示我上面寫的對話框。 – Sajmon

+0

你的意思是在AsyncTask之外調用它嗎? 我需要從我的ASynTask –

3

上述夏威夷的答案是正確的。

更具體地說,doInBackground方法是而不是與UI線程同步。因此,你不應該在這種方法中更新你的視圖/佈局/任何與UI相關的東西。我建議你show()你的對話在AsyncTask的執行之前。

+0

這個答案100%不正確。 'onPreExecute()'也在UI線程中調用,用於更新UI元素:http://developer.android.com/reference/android/os/AsyncTask.html – Devunwired

+0

@Devunwired,感謝您支持我:)。 StackOverflow noobs真的很討厭我今天哈哈 –

+0

作爲@AlexLockwood說.. doInBackground(..)是爲了(在後臺做)..對話框shold顯示在onPreExecute(),UI應更新onPostExecute(.. )。如果你需要在doInBackground(..)這個長/繁重的代碼中更新UI,比如更新進度條(UI的一部分),你可以從onProgressUpdate(..).. – Ewoks

相關問題