0

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
Wilduck
  • 13,822
  • 10
  • 58
  • 90
user2137817
  • 1,825
  • 5
  • 28
  • 45

2 Answers2

0

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());
Community
  • 1
  • 1
DigCamara
  • 5,540
  • 4
  • 36
  • 47
  • 19:21:11.524: E/AndroidRuntime(333): FATAL EXCEPTION: main 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)& no it can't be the lineChart.setChartData(array1,array2,xd); because it works before i add the screenShot code – user2137817 Apr 01 '13 at 18:54
  • i added the logcat to my question – user2137817 Apr 01 '13 at 19:12
  • I tried your code.It works on everything except the custom view, i always get the NPE on this line bitmap=Bitmap.createBitmap(v1.getDrawingCache()); – user2137817 Apr 02 '13 at 10:16
  • my guess would be that you're overriding something which makes the view behave differently. Could you post the relevant post please? (Declaration of your custom view and whatever methods of View you chose to override) – DigCamara Apr 02 '13 at 15:46
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().

praneetloke
  • 1,953
  • 1
  • 14
  • 15
  • I already enabled the drawing cache v1.setDrawingCacheEnabled(true); – user2137817 Apr 02 '13 at 09:36
  • hmm did you update your question or something? I didn't see that there at first which is why it seemed obvious to me that turning on the drawing cache would probably solve the problem. – praneetloke Apr 02 '13 at 13:59
  • No i didn't,it's there from the beginning – user2137817 Apr 02 '13 at 14:07
  • I must have missed it then. Anyway, if v1.getDrawingCache() is null then according to the documentation, drawing cache is not enabled. Perhaps, even when you set the flag to true, it doesn't enable the cache. Once again, according to the documentation for setDrawingCacheEnabled(boolean), it says the flag will be ignored if you have hardware acceleration turned on. Did you check if you have that on? What platform version are you running this on? – praneetloke Apr 02 '13 at 14:33