2012-01-11 76 views
2

我正在嘗試使用Robotium截取我的Android應用程序,我正在使用以下功能,我發現hereAndroid,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上線,它看起來我好像觀爲null。

我也使用

solo.getViews()審判;

並循環瀏覽每個視圖並截取屏幕截圖,但是我也得到一個NullPointerException。

ArrayList views = solo.getViews(); 

for(int i=0; i < views.size(); i++) 
{ 
    takeScreenShot(solo.getView(i), "Test"); 
} 

我使用Robotium已經夠新到Android & 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) 

回答

4

爲什麼您得到NullPointerException異常的原因是因爲你使用getView(INT ID)不正確。因爲你給它一個索引而不是id,它不會找到你正在查找的視圖,並返回null。你想使用什麼是以下幾點:

takeScreenShot(solo.getViews()得到(0),「測試」。)

這意味着在給定時間提供給Robotium所有意見的第一個視圖。

+0

我已經嘗試過這種方式還可以,但我還是看到被我稱之爲takeScreenShot行NullPointerException異常。任何其他方式我可以確定我的視圖是否存在? – 2012-01-12 13:17:30

+0

如果你做了一個solo.getViews()。size(),你會得到什麼? – Renas 2012-01-12 13:32:56

+0

返回14. – 2012-01-12 14:08:20

0

確保您的模擬器爲SD卡預留了幾兆字節。

如果你想拉JPG回你的電腦,你可以得到的Java運行這個命令行:

C:\用戶\我\ Android的軟件開發工具包\平臺工具\ adb.exe拉 /sdcard/test_1329402481933.jpg C:\

0

對於拍攝畫面射擊在應用程序的任何點剛寫這段代碼

solo.takeScreenshot();

但不要忘記在你的主應用程序中給予權限。