0

我正在尝试截取我正在绘制的图表的屏幕截图,但每次尝试我都会得到一个NullExceptionPointer.

这是我的代码:

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

lineChart自定义视图在哪里

此 logcat 将此行指示为 null:bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 这是我的 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
4

2 回答 2

0

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().

于 2013-04-01T19:48:51.183 回答
0

我没有使用过getRootView,但文档说明了它

返回包含此视图的最顶层视图

如果它本身就是最顶层的 View,它将返回 null。尝试

View v1 = (View)lineChart;

反而。

文档(和从这里),我想你可以通过添加来强制构建绘图缓存

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

在你得到位图之前

bitmap = Bitmap.createBitmap(v1.getDrawingCache());
于 2013-04-01T18:12:36.300 回答