应用程序自动退出,没有任何警告或错误

Posted

技术标签:

【中文标题】应用程序自动退出,没有任何警告或错误【英文标题】:Application exit automatically without any warning or error 【发布时间】:2012-02-23 08:59:19 【问题描述】:

我已经使用 tesseract 库开发了 OCR 应用程序,

应用程序在执行以下代码时退出:

/*...
... Other Code stuff
...*/
    protected Boolean doInBackground(String... arg0) 

    /*...
    ... Other Code stuff
    ...*/
    Pix pix = getPixFromBitmap(bitmap);
    pix = preprocess(pix);
    Pixa pixa = slice(pix); // Code Updated

    try 
        baseApi.setPageSegMode(TessBaseAPI.PSM_SINGLE_LINE);

        int num = pixa.size();

        for (int i = 0; i < num; i++) 
            Pix pixi = pixa.getPix(i);

            /*...
            ... Other Code stuff
            ...*/
        
        pixa.recycle();
        baseApi.end();

     catch (RuntimeException e) 
Log.e("OcrRecognizeAsyncTask","Caught RuntimeException in request to Tesseract. Setting state to CONTINUOUS_STOPPED.");
        e.printStackTrace();
        try 
            // baseApi.clear();
            activity.stopHandler();
         catch (NullPointerException e1) 
            // Continue
        
        return false;
    

    private Pixa slice(Pix pix) 
    HydrogenTextDetector htd = new HydrogenTextDetector();
    HydrogenTextDetector.Parameters hydrogenParams = htd.getParameters();
    hydrogenParams.debug = false;
    hydrogenParams.skew_enabled = true;
    htd.setParameters(hydrogenParams);

    htd.setSourceImage(pix);
    pix.recycle();
    htd.detectText();
    Pixa unsorted = htd.getTextAreas();
    Pixa pixa = unsorted.sort(Constants.L_SORT_BY_X, Constants.L_SORT_DECREASING);
    unsorted.recycle();
    htd.clear();
    return pixa;

Logcat详情如下:

02-23 13:37:09.986: I/WindowManager(102): Setting rotation to 0, animFlags=1
02-23 13:37:10.006: I/ActivityManager(102): Config changed:  scale=1.0 imsi=405/30 loc=en_IN touch=3 keys=1/1/2 nav=3/1 orien=1 layout=17 uiMode=17 seq=33
02-23 13:37:10.116: I/UsageStats(102): Unexpected resume of com.htc.launcher while  already resumed in edu.sfsu.cs.orange.ocr
02-23 13:37:10.816: W/InputManagerService(102): Got RemoteException sending setActive(false) notification to pid 4880 uid 10062

我将 Bitmap 转换为 Leptonica Pix 对象,而不是将 Pix 转换为 Leptonica Pixa 对象,而不是进行 OCR 重组。

在处理过程中,有时 OCR 重组成功完成,有时 android 主屏幕自动出现。

我不知道这背后的问题是什么,为什么会出现 RemoteException 警告?

请帮我解决这个问题。

谢谢,

【问题讨论】:

您在哪一行看到异常? 这就是问题所在,在应用程序执行过程中没有抛出错误,但它会自动显示主屏幕,LogCat 只显示一个警告 W/InputManagerService(102): Got RemoteException 发送 setActive(false) 通知到 pid 4880 uid 10062 这个警告(W/InputManagerService 可能没有任何意义,因为我经常在一个运行良好的 opengl 应用程序中得到它。但上面提到的另一个异常更有趣。 【参考方案1】:

您不能在 AsyncTask 之间结束 baseApi。这就是您遇到崩溃问题的问题。

你使用了以下代码,

pixa.recycle();
baseApi.end();

评论以下行,然后尝试运行应用程序,它可能会解决您的问题。

pixa.recycle();
// baseApi.end();

================================================ ====================================

自动清空堆内存可以实现如下代码:

public void onCreate(Bundle savedInstanceState) 

    clearMemoryCountDownTimer mClearMemoryCountDownTimer = new clearMemoryCountDownTimer(5000, 5000).start();

private class clearMemoryCountDownTimer extends CountDownTimer 

    public clearMemoryCountDownTimer(long millisInFuture,
            long countDownInterval) 
        super(millisInFuture, countDownInterval);
    

    @Override
    public void onFinish() 
        start();
        Log.i(TAG, "Timer Finished");
    

    @Override
    public void onTick(long millisUntilFinished) 
        if (Debug.getNativeHeapFreeSize() < 100000) 
            clearMemory();
            Log.i(TAG, "requiredClearMemory");
        
        Log.i(TAG, "getNativeHeapSize : " + Debug.getNativeHeapSize());
        Log.i(TAG,
                "getNativeHeapFreeSize : " + Debug.getNativeHeapFreeSize());
        Log.i(TAG,
                "getNativeHeapAllocatedSize : "
                        + Debug.getNativeHeapAllocatedSize());

    

    void clearMemory() 
        Log.i(TAG, "System.gc()-Start");
        System.gc();
        Log.i(TAG, "System.gc()-End");
    

【讨论】:

以上是关于应用程序自动退出,没有任何警告或错误的主要内容,如果未能解决你的问题,请参考以下文章

安装oracle client 11g时 自动退出,怎么解决?

设置shell脚本执行错误自动退出

shell脚本怎么设置语句出错自动退出?(set -eset -o errexit)使用未初始化的变量自动退出?(set -uset -o nounset)(退出脚本)

WIn10 64位安装windows Mobile 设备中心不能启动或自动退出

shell脚本怎么设置语句出错自动退出?(set -eset -o errexit)使用未初始化的变量自动退出?(set -uset -o nounset)(退出脚本)shell set

转 C#WinForm程序异常退出的捕获继续执行与自动重启