Question

I'm trying to take a screenshot of my Android application using Robotium, I'm using the below function that I found here.

public static String SCREEN_SHOTS_LOCATION="/sdcard/"; 

public static void takeScreenShot(View view, String name) throws Exception 
{ 
    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(); 
    Bitmap b = view.getDrawingCache(); 
    FileOutputStream fos = null; 

    try 
    { 
         File sddir = new File(SCREEN_SHOTS_LOCATION); 

         if (!sddir.exists()) 
         { 
             sddir.mkdirs(); 
         } 
         fos = new FileOutputStream(SCREEN_SHOTS_LOCATION + name + "_" + System.currentTimeMillis() + ".jpg"); 

         if (fos != null) 
         { 
             b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
             fos.close(); 
         } 
     } 
     catch (Exception e) 
     { 
     } 
} 

I'm calling it like this from my test:

takeScreenShot(solo.getView(0), "Test");

When I call the function, I get a NullPointerException on that line, it looks to me as if the View is null.

I've also tried using

solo.getViews();

and cycling through each view and taking a screenshot, but I get a NullPointerException for each also.

ArrayList views = solo.getViews();

for(int i=0; i < views.size(); i++)
{
    takeScreenShot(solo.getView(i), "Test");
}

I'm new enough to Android & Android test automation using Robotium, can anybody give me some advice on debugging this, or the reason why Views seem to be null and my screen captures don't work?

TIA.

Update

Error in testUI:
java.lang.NullPointerException
        at com.myapp.test.UITests.testUI(UITests.java:117)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
        at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
        at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
Was it helpful?

Solution

The reason why you are getting NullPointerException is because you are using getView(int id) incorrectly. As you are giving it an index instead of id, it will not find the view that you are looking for and thus returns null. What you want to use is following:

takeScreenShot(solo.getViews().get(0), "Test")

Which means the first view of all the views available to Robotium at a given time.

OTHER TIPS

Make sure your emulator has some megabytes set aside for the SD card.

If you want to pull the jpg back to your PC, you can get java to run this command line:

C:\Users\Me\android-sdks\platform-tools\adb.exe pull /sdcard/test_1329402481933.jpg c:\

For taking screen shot at any point of the application Just write this piece of code

solo.takeScreenshot();

But don't forget to give permission in your main Application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top