Android的,Robotium - 问题采取截图 [英] Android, Robotium - Issue taking screenshot

查看:166
本文介绍了Android的,Robotium - 问题采取截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的使用Robotium Android应用程序的截图,我使用下面的函数,我发现的这里

 公共静态字符串SCREEN_SHOTS_LOCATION =/ SD卡/;公共静态无效takeScreenShot(查看视图,字符串名称)抛出异常
{
    view.setDrawingCacheEnabled(真);
    view.buildDrawingCache();
    位图B = view.getDrawingCache();
    FOS的FileOutputStream = NULL;    尝试
    {
         文件sddir =新的文件(SCREEN_SHOTS_LOCATION);         如果(!sddir.exists())
         {
             sddir.mkdirs();
         }
         FOS =新的FileOutputStream(SCREEN_SHOTS_LOCATION +名称+_+ System.currentTimeMillis的()+.JPG);         如果(FOS!= NULL)
         {
             b.com preSS(Bitmap.Com pressFormat.JPEG,90,FOS);
             fos.close();
         }
     }
     赶上(例外五)
     {
     }
}

我打电话像这样从我的测试:

  takeScreenShot(solo.getView(0),测试);

当我调用函数,我得到在该行一个NullPointerException异常,在我看来,如果视图为null。

我使用也试过


  

solo.getViews();


和通过每个视图循环,并采取截图,但我得到的每个也NullPointerException异常。

  ArrayList的意见= solo.getViews();的for(int i = 0; I< views.size();我++)
{
    takeScreenShot(solo.getView(i)中,测试);
}

我是新足以Android和放大器;使用Android的Robotium测试自动化,任何人可以给我一些建议在调试这个,或者为什么观点似乎有理由为空,我的屏幕捕获不工作?

TIA。

更新

 错误testUI:
显示java.lang.NullPointerException
        在com.myapp.test.UITests.testUI(UITests.java:117)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
        在android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
        在android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
        在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
        在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
        在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
        在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1448)


解决方案

为什么您得到NullPointerException异常的原因是因为您正在使用getView(INT ID)不正确。当你给它一个指标,而不是ID,也不会发现你正在寻找的视图,从而返回null。你想使用什么是以下几点:

takeScreenShot(solo.getViews()得到(0),测试)

这意味着在给定时间提供给所有Robotium的意见,第一种观点。

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)

解决方案

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.

这篇关于Android的,Robotium - 问题采取截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆