Android、Robotium - 截屏时出现问题

发布于 2024-12-25 21:55:13 字数 2343 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2025-01-01 21:55:13

您收到 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.

葵雨 2025-01-01 21:55:13

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

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

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

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:\

绮烟 2025-01-01 21:55:13

要在应用程序的任何位置截取屏幕截图,只需编写这段代码

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文