我正在尝试使用 Robotium 截取我的 Android 应用程序的屏幕截图,我正在使用我发现的以下函数 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)
{
}
}
我在测试中这样调用它:
takeScreenShot(solo.getView(0), "Test");
当我调用该函数时,我在该行收到一个 NullPointerException,在我看来, View 好像为 null。
我也尝试过使用
solo.getViews();
并循环浏览每个 View 并截取屏幕截图,但每个 View 也出现 NullPointerException。
ArrayList views = solo.getViews();
for(int i=0; i < views.size(); i++)
{
takeScreenShot(solo.getView(i), "Test");
}
我对使用 Robotium 进行 Android 和 Android 测试自动化还很陌生,任何人都可以给我一些关于调试此问题的建议,或者 View 似乎为空且我的屏幕截图不起作用的原因吗?
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 时,它不会找到您正在查找的 View ,因此返回 null。您要使用的内容如下:
takeScreenShot(solo.getViews().get(0), "测试")
这意味着在给定时间 Robotium 可用的所有 View 中的第一个 View 。
关于Android、Robotium - 截屏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819542/