传入通知使我的应用程序崩溃 Android

Incoming notifications are crashing my app Android

使用我的 Android 应用程序时,一切正常,但一旦我收到传入通知(来自其他应用程序),我的应用程序就会重新启动。 只有当我的相机处于活动状态并且在此期间我也在 canvas 视图上绘图时,它才会这样做。

据我所知,在某些过程中无法禁用通知(f.e 记录)所以我需要以某种方式修复它,但我得到的唯一错误日志如下:

   --------- beginning of crash
2019-01-23 23:33:59.712 7469-7469/com.myApp.com E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.myApp.com, PID: 7469
    java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@cafcfb0
        at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1271)
        at android.graphics.Canvas.drawBitmap(Canvas.java:1368)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:973)
        at com.author.myApp.Songplayer.Managers.CanvasDraw.getResizedBitmap(CanvasDraw.java:53)
        at com.author.myApp.Songplayer.Managers.CanvasDraw.onDraw(CanvasDraw.java:38)
        at android.view.View.draw(View.java:17236)
        at android.view.View.updateDisplayListIfDirty(View.java:16201)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1254)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.draw(View.java:17239)
        at android.view.View.updateDisplayListIfDirty(View.java:16201)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.draw(View.java:17239)
        at android.view.View.updateDisplayListIfDirty(View.java:16201)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.updateDisplayListIfDirty(View.java:16196)
        at android.view.View.draw(View.java:17002)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3777)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3560)
        at android.view.View.draw(View.java:17239)
        at com.android.internal.policy.DecorView.draw(DecorView.java:801)
        at android.view.View.updateDisplayListIfDirty(View.java:16201)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:677)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:683)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:797)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2991)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2785)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2376)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1366)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6768)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:926)
        at android.view.Choreographer.doCallbacks(Choreographer.java:735)
        at android.view.Choreographer.doFrame(Choreographer.java:667)
2019-01-23 23:33:59.712 7469-7469/com.myApp.com E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:912)
        at android.os.Handler.handleCallback(Handler.java:761)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
2019-01-23 23:33:59.742 542-8728/? I/ImagingSystem: virtual TRetCode hw::CIMX219Spec::getOTPTestResult(): OTP module_id=0xc8,vendor_id=0x31,checksum=0x1
2019-01-23 23:33:59.746 542-8714/? I/ProjectName: Tornado: mrc_cv_ColorSaturationDetectionTOA:UISaturationLevel:0,saturationCompValid:0,outPutSaturationComp:256.
2019-01-23 23:33:59.775 542-8728/? I/ImagingSystem: virtual TRetCode hw::CIMX219Spec::getOTPTestResult(): OTP module_id=0xc8,vendor_id=0x31,checksum=0x1
2019-01-23 23:33:59.779 542-8714/? I/ProjectName: Tornado: mrc_cv_ColorSaturationDetectionTOA:UISaturationLevel:0,saturationCompValid:0,outPutSaturationComp:256.
2019-01-23 23:33:59.802 1218-4278/? E/ReportTools: This is not beta user build

基本上什么都没告诉我.. 只是在通知后找不到 canvas。

我不确定我应该做什么。当我检查 Android 的相机如何工作时,他们禁用了声音和振动,所以也许这对我有帮助?如果是,那么我已经搜索了一段时间,但没有找到禁用其他通知的 vibration/sound 的方法。

编辑

添加 onDraw class:

public class CanvasDraw extends View{

    Bitmap voiceMeterChart;
    Paint linePaint = new Paint();

    public CanvasDraw(Context context) {
        super(context);
        voiceMeterChart = BitmapFactory.decodeResource(getResources(), R.drawable.voice_chart_meter);
        voiceMeterChart = adjustOpacity(voiceMeterChart, 125);


        linePaint.setColor(Color.RED);
        linePaint.setStrokeWidth(1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int canvasWidth = canvas.getWidth();
        int canvasHeight = canvas.getHeight();


        canvas.drawBitmap(getResizedBitmap(voiceMeterChart, (canvasWidth * 0.1), canvasHeight, canvasWidth), 25, 0, null);

    }

    public Bitmap getResizedBitmap(Bitmap bm, double newWidth, int newHeight, int canvasWidth) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }

    private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
    {
        Bitmap mutableBitmap = bitmap.isMutable()
                ? bitmap
                : bitmap.copy(Bitmap.Config.ARGB_8888, true);
        Canvas canvas = new Canvas(mutableBitmap);
        int colour = (opacity & 0xFF) << 24;
        canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
        return mutableBitmap;
    }
}

并且在日志中显示它在这一行崩溃:

 Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

第一个 onDraw 调用回收 getResizedBitmap 中的 voiceMeterChart 位图。

// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
bm.recycle();

当再次调用 onDraw 时,它会尝试使用原始位图创建另一个位图,但它已不存在。