Android,Robotium-问题截图



我正在尝试使用Robotium截屏我的Android应用程序,我正在使用我在这里找到的以下功能。

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) 
     { 
     } 
} 

我在测试中这样称呼它:

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

当我调用该函数时,我在那一行得到一个NullPointerException,在我看来,它就像View是null。

我也试过使用

solo.getViews();

循环浏览每个视图并截屏,但每个视图都会出现NullPointerException。

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

我对安卓已经足够陌生了;使用Robotium的Android测试自动化,有人能给我一些调试建议吗?或者为什么视图似乎为空,我的屏幕截图不起作用?

TIA。

更新

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)

获得NullPointerException的原因是您使用getView(int id)不正确。由于您给它一个索引而不是id,它将找不到您要查找的视图,因此返回null。您想要使用的是:

takeScreenShot(solo.getViews().get(0),"测试")

这意味着Robotium在给定时间可用的所有视图中的第一个视图。

确保模拟器为SD卡预留了一些兆字节。

如果你想把jpg拉回到你的电脑上,你可以让java运行这个命令行:

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

在应用程序的任何一点进行屏幕截图只需编写以下代码

solo.takeScreenshot();

但不要忘记在主应用程序中授予权限。

最新更新