收到的通知使我的应用Android崩溃

时间:2019-01-23 21:38:48

标签: android android-studio push-notification notifications android-notifications

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

据我所知,在某些过程中(例如录制)无法禁用通知,因此我需要以某种方式对其进行修复,但是我得到的唯一错误日志如下:

   --------- 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

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

我不确定该怎么办。当我检查Android的相机的工作方式时,它们会禁用声音和振动,所以也许对我有帮助?如果是,那么我已经搜索了很长时间,但是没有找到一种方法来禁用其他通知的振动/声音。

编辑

添加onDraw类:

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);

1 个答案:

答案 0 :(得分:2)

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

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

再次调用onDraw时,它将尝试使用原始位图创建另一个位图,并且该位图不再存在。