Error when taking a screenshot in Android

2019-09-06 13:38发布

问题:

I'm trying to take a screen shot of a chart I'm drawing, but everytime I try I get a NullExceptionPointer.

Here is my code :

lineChart.setChartData(array1,array2,xd);
View v1 = lineChart.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

Where lineChart is a custom View

This logcat indicates this line as null: bitmap = Bitmap.createBitmap(v1.getDrawingCache()); here is my logcat :

04-01 19:21:11.524: E/AndroidRuntime(333): FATAL EXCEPTION: main

04-01 19:21:11.524: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appui/com.example.appui.CompareActivity}: java.lang.NullPointerException
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)

04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-01 19:21:11.524: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:21:11.524: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-01 19:21:11.524: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
04-01 19:21:11.524: E/AndroidRuntime(333): Caused by: java.lang.NullPointerException
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.graphics.Bitmap.createBitmap(Bitmap.java:367)
04-01 19:21:11.524: E/AndroidRuntime(333):  at com.example.appui.CompareActivity.onCreate(CompareActivity.java:37)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-01 19:21:11.524: E/AndroidRuntime(333):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-01 19:21:11.524: E/AndroidRuntime(333):  ... 11 more

回答1:

I haven't used getRootView, but the documentation states it

Returns the topmost view containing this view

If it is the topmost View by itself, it would return null. Try

View v1 = (View)lineChart;

instead.

From the documentation (and from here) , I guess you could force the Drawing Cache to be built by adding

    // this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
   v1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
   v1.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
   v1.buildDrawingCache();

before you get the bitmap with

bitmap = Bitmap.createBitmap(v1.getDrawingCache());


回答2:

The documentation for View.getDrawingCache(boolean) states:

Returns the bitmap in which this view drawing is cached. The returned bitmap is null when caching is disabled.

Set the drawing cache to true using setDrawingCacheEnabled(boolean). You can also check if the drawing cache is enabled using isDrawingCacheEnabled().