getDrawingCache()始终返回null

时间:2012-08-27 21:17:37

标签: android nullpointerexception android-view android-notifications

我知道这个问题经常被问到,但这似乎有所不同。我已经尝试了StackOverflow的6个解决方案但它没有用。我试图在他们离开应用程序时将某人制作成Notification.BigPictureStyle

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        _thread.setRunning(true);
        _thread.start();            
        setDrawingCacheEnabled(true);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        _thread.setRunning(false);

        measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        layout(0, 0, getMeasuredWidth(), getMeasuredHeight());

        buildDrawingCache(false);
        Bitmap currentState = Bitmap.createBitmap(getDrawingCache());
        setDrawingCacheEnabled(false);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(DrawActivity.this, DrawActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(DrawActivity.this, 0, notificationIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Notification noti = new Notification.BigPictureStyle(
                new Notification.Builder(DrawActivity.this)
                    .setContentTitle("Your drawing")
                    .setContentText("Unfold to see drawing")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(contentIntent))
        .setSummaryText("Here you can see how you left your drawing.")
        .bigPicture(currentState)
        .build();

        manager.notify(1023, noti);

    }
}

但我总是在这一行得到NullPointerException

Bitmap currentState = Bitmap.createBitmap(getDrawingCache());

此代码来自StackOverflow,启用了所有这些缓存。有人能看出什么问题吗?

顺便说一下,我当然正在使用Android 4.1.1。

1 个答案:

答案 0 :(得分:0)

类似问题here更多信息here ...

问题似乎是你无法访问surfaceview的内容 - 显然你可以捕获ImageView的内容......我假设你使用了SurfaceView,因此遇到了同样的问题...如果你愿意挖掘进一步this可能有所帮助(来自上述主题/主题之一)

如果你没有使用surfaceview,那么setLayerType可能就是你的答案......

相关问题