我试图用Robotium来截取我的安卓应用程序的截图,我使用的是我找到的这里的以下功能。
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,在我看来,视图似乎是空的。
我也试过用
solo.getViews();
骑自行车穿过每一个视图,拍一个截图,但我也得到了一个NullPointerException。
ArrayList views = solo.getViews();
for(int i=0; i < views.size(); i++)
{
takeScreenShot(solo.getView(i), "Test");
}我对Android和Android的自动化测试已经足够了,有人能给我一些关于调试这个的建议吗?或者为什么视图看起来是空的,而我的屏幕截图却不能工作呢?
蒂娅。
更新
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)发布于 2012-01-12 06:12:13
获取NullPointerException的原因是使用getView(int )不正确。当您给它一个索引而不是id时,它将找不到您要查找的视图,因此返回null。您要使用的内容如下:
takeScreenShot(solo.getViews().get(0),"Test")
这意味着在给定的时间内,所有视图的第一个视图。
发布于 2012-02-16 15:08:45
确保您的模拟器为SD卡预留了一些兆字节。
如果要将jpg拉回PC,可以让java运行以下命令行:
C:\Users\Me\android-sdks\platform-tools\adb.exe拉/sdcard/test_1329402481933.jpg c:\
发布于 2013-05-05 14:47:15
若要在应用程序的任意点拍摄屏幕快照,只需编写以下代码即可。
solo.takeScreenshot();
但别忘了在你的主要申请中给予许可。
https://stackoverflow.com/questions/8819542
复制相似问题