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 ,在我看来,视图好像为空。
我也尝试过使用
solo.getViews();
循环浏览每个视图并截取屏幕截图,但每个视图也出现 NullPointerException 。
ArrayList views = solo.getViews();
for(int i=0; i < views.size(); i++)
{
takeScreenShot(solo.getView(i), "Test");
}
我对 Android 和 Android 还很陌生。使用 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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您收到 NullPointerException 的原因是因为您错误地使用了 getView(int id) 。当您给它一个索引而不是 id 时,它不会找到您正在查找的视图,因此返回 null。您要使用的内容如下:
takeScreenShot(solo.getViews().get(0), "Test")
这意味着在给定时间 Robotium 可用的所有视图中的第一个视图。
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.
确保您的模拟器为 SD 卡预留了一些兆字节。
如果你想将 jpg 拉回你的电脑,你可以让 java 运行这个命令行:
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:
要在应用程序的任何位置截取屏幕截图,只需编写这段代码
solo.takeScreenshot();
但不要忘记在您的主应用程序中授予许可。
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.