Android 视图中createBitmap()中的NullPointerException

Android 视图中createBitmap()中的NullPointerException,android,view,nullpointerexception,drawable,Android,View,Nullpointerexception,Drawable,我使用以下代码从视图中获取位图: private static Bitmap loadBitmapFromView(View yourView) { Bitmap snapshot = null; Drawable drawable = null; yourView.setDrawingCacheEnabled(true); yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUA

我使用以下代码从
视图中获取
位图

private static Bitmap loadBitmapFromView(View yourView) {
        Bitmap snapshot = null;
        Drawable drawable = null;
        yourView.setDrawingCacheEnabled(true);
        yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashot
        try {
            snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
            drawable = new BitmapDrawable(snapshot);
        } finally {
            yourView.setDrawingCacheEnabled(false);
        }
        return snapshot;
    }
但是我在下面的一行中得到了
NullPointerException

snapshot=Bitmap.createBitmap(yourView.getDrawingCache())

这是
Logcat

09-19 15:39:15.481: E/AndroidRuntime(8704): FATAL EXCEPTION: main
09-19 15:39:15.481: E/AndroidRuntime(8704): java.lang.NullPointerException
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.graphics.Bitmap.createBitmap(Bitmap.java:455)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.utils.MapUtility.loadBitmapFromView(MapUtility.java:85)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.utils.MapUtility.createMarker(MapUtility.java:74)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.scb.bonuspartner.offeractivity.fragments.MapOfferFragment.onLocationChanged(MapOfferFragment.java:70)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.os.Looper.loop(Looper.java:154)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at android.app.ActivityThread.main(ActivityThread.java:4624)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at java.lang.reflect.Method.invoke(Method.java:511)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
09-19 15:39:15.481: E/AndroidRuntime(8704):     at dalvik.system.NativeStart.main(Native Method)
代码应该是

snapshot = snapshot.createBitmap(yourView.getDrawingCache());
因为您已经将快照转换为位图

Bitmap snapshot = null;

代码是否应为

snapshot = snapshot.createBitmap(yourView.getDrawingCache());
因为您已经将快照转换为位图

Bitmap snapshot = null;
试着这样做

private static Bitmap loadBitmapFromView(View yourView) {
        Bitmap snapshot = null;
        Drawable drawable = null;
        yourView.setDrawingCacheEnabled(true);
        yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashot
        try {
            yourView.buildDrawingCache(); // UPDATE HERE
            snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
            drawable = new BitmapDrawable(snapshot);
        } finally {
            yourView.setDrawingCacheEnabled(false);
        }
        return snapshot;
    }
说明:

对于知识位图是最后一个类。因此,垃圾收集器(GC)的优先级非常低,而且内存非常昂贵。默认情况下,视图不会从画布创建位图,因为如果这样做,将在内存中为每个视图生成一个位图,并可能导致buildDrawingCache膨胀。因此,提供了
buildDrawingCache()
方法,您可以在需要时生成位图

这样试试

private static Bitmap loadBitmapFromView(View yourView) {
        Bitmap snapshot = null;
        Drawable drawable = null;
        yourView.setDrawingCacheEnabled(true);
        yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashot
        try {
            yourView.buildDrawingCache(); // UPDATE HERE
            snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
            drawable = new BitmapDrawable(snapshot);
        } finally {
            yourView.setDrawingCacheEnabled(false);
        }
        return snapshot;
    }
说明:


对于知识位图是最后一个类。因此,垃圾收集器(GC)的优先级非常低,而且内存非常昂贵。默认情况下,视图不会从画布创建位图,因为如果这样做,将在内存中为每个视图生成一个位图,并可能导致buildDrawingCache膨胀。因此,提供了
buildDrawingCache()
方法,您可以在需要时生成位图

请附加logcat Log以便空指针为yourView.getDrawingCache?尝试输出视图,看看它是否也是空的。在方法的开头检查。是否导入了正确的位图库?另外,您是否检查了以下答案:@JackalopeZero视图存在,它不是
null
请附加logcat logo以便null指针是yourView.getDrawingCache?尝试输出视图,看看它是否也是空的。在方法的开头检查。是否导入了正确的位图库?另外,您是否检查了以下答案:@JackalopeZero该视图存在,它不是
null
@JackalopeZero:no。createBitmap是Bitmap类的静态方法。可以在实例上调用它,但不建议这样做,尤其是在这种情况下,因为此时snapshot为null,这将导致另一个NPE。@JackalopeZero:否。createBitmap是Bitmap类的静态方法。可以在实例上调用它,但不建议这样做,尤其是在这种情况下,因为快照此时为null,这将导致另一个NPE.yourView.buildDrawingCache();是为我工作,但你能解释一下吗?@Dhamodharan我已经添加了解释来回答请现在查看YourView.buildDrawingCache();是为我工作,但你能解释一下吗?@Dhamodharan我已经补充了解释,请现在看