2016-07-06 57 views
1

這裏是我的Maven項目,我試圖測試谷歌地圖活動。錯誤膨脹類的片段。無法獲取FragmentManager在谷歌播放服務

... 
    <dependency> 
     <groupId>com.almende.eve</groupId> 
     <artifactId>eve-bundle-android-ws</artifactId> 
     <version>3.1.1</version> 
    </dependency> 
... 

我使用3型

<dependency> 
    <groupId>com.google.android.gms</groupId> 
    <artifactId>play-services</artifactId> 
    <version>x.y.z</version> 
    <scope>compile</scope>  
    <type>aar</type> 
</dependency> 

如果X.Y.Z是8.3.0版本然後有

android.view.InflateException: XML file ./src/main/res/layout/main.xml line #-1 (sorry, not yet implemented): Error inflating class fragment 
     at com.eduonix.app.EduonixActivityTest.setup(EduonixActivityTest.java:38) 
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f070028 
     at com.eduonix.app.EduonixActivityTest.setup(EduonixActivityTest.java:38) 

這就是在ID R類別對應common_google_play_services_install_text_phone constats。

如果X.Y.Z是7.0.0然後讓

java.lang.NoSuchFieldError: MapAttrs_ambientEnabled 
at com.eduonix.app.EduonixActivityTest.setup(EduonixActivityTest.java:38) 

Android SDK中是22,8.3.0是比較正確的,但同樣的錯誤顯示爲9.2.0,以及。

我的活動是

public class EduonixActivity extends android.support.v4.app.FragmentActivity { 

     protected com.google.android.gms.maps.GoogleMap map; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); System.out.println("hello!"); 
      android.support.v4.app.FragmentManager fM = getSupportFragmentManager(); // error line 
      System.out.println("FragmentManager is initiated"); // this line is not achieved 
      SupportMapFragment fm = (SupportMapFragment)fM.findFragmentById(R.id.map); 
      System.out.println("fm is initiated"); 
      map = fm.getMap(); 
      System.out.println("map is initiated"); 
     } 
    } 

Maven構建流程被打破時,試圖測試蒙山Robolectic框架

@Config(manifest = "src/main/AndroidManifest.xml") 
    @RunWith(RobolectricTestRunner.class) 
    public class EduonixActivityTest { 

     private EduonixActivity activity; 

     @Before 
     public void setup() { 
      activity = Robolectric.buildActivity(EduonixActivity.class).create().start().get();// error 
     } 

     @Test 
     public void testEduonixActivity() { 
      assertThat(activity).isNotNull(); 
     } 
    } 

的AndroidManifest.xml是

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      android:versionCode="2" 
      android:versionName="1.0.0-SNAPSHOT" 
      package="com.eduonix.app"> 

    <uses-sdk android:minSdkVersion="22" 
       android:targetSdkVersion="22"/> 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <!-- External storage for caching. --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <!-- My Location --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
     <!-- Maps API needs OpenGL ES 2.0. --> 
    <uses-feature 
      android:glEsVersion="0x00020000" 
      android:required="true"/> 
    <application android:name="com.eduonix.app.EduonixApplication"> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
    <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="@string/google_maps_key" /> 
     <activity android:name=".EduonixActivity" android:screenOrientation="portrait"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 


    </application> 
</manifest> 

任何想法將不勝感激。

+0

問題是Robolectric無法找到AndroidManifest和/或資源。如果是灰燼構建,它會更容易處理,因爲我有很多經驗。如果Robolectric解析了你的'AndroidManifest',我會放一些日誌記錄來理解。我還會看看'RobolectricGradleTestRunner'與'RobolectricTestRunner'有區別 –

+0

我真的不知道如何解析AndroidManifest到Robolectric,它位於主文件夾中,或者您可能假設測試的清單?我測試了FragmentActivity,並從Activity中擴展了一個活動,並且構建得很好。我沒有提到google_play_version,它實際上低於android的預期。 –

+0

@RunWith(RobolectricGradleTestRunner.class)沒有幫助,它已被棄用。 –

回答

0

在比較android.app.Fragment和andoriod.app.support.v4.Fragment之後,答案是:Robolectic不能誇大Fragment,因爲Google服務不可用。因此,一個解決方案是指定片段吹氣這樣

public class MyFragment extends com.google.android.gms.maps.SupportMapFragment{ 
static private MyFragment fragment = new MyFragment(); 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_stub_for_test, container); 
    return view; 
    } 
// public constructor must be empty 
static public MyFragment getInstance(){ 
    return fragment; 
    } 
} 

其中測試存根可以佈局一些XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    tools:context="com.example.zheka.gmapexample1.MapsActivity" > 

    <TextView 
     android:id="@+id/tvDisplayText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="No Passed Bundle :(" /> 
    </LinearLayout> 

當然,Android的部署這種指定片段必須SupportMapFragment更換:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" 
    tools:context="com.example.zheka.gmapexample1.MapsActivity" > 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" />  
</LinearLayout> 

其中android:name=pkgName.MyFragment用於測試模式。對於測試和部署我指定MyTestRunner類@RunWith,以便確定像的src /測試/ RES測試資源文件夾

@Config(manifest = "src/main/AndroidManifest.xml" 
    ,shadows = { 
     //ShadowActivity.class 
     //,ShadowGooglePlayServicesUtil.class 
     //ShadowMapsActivity.class 
     } 
) 
@RunWith(RobolectricTestRunner.class) 
//@RunWith(MyTestRunner.class) 
public class ExampleUnitTest { 
    MapsActivity activity; 
    android.support.v4.app.Fragment fragment; 

    @Before 
    public void setup(){ 
     activity = Robolectric.buildActivity(MapsActivity.class) 
       .create() 
       .start() //no activity error can happen, if so just commment 
       .get(); 
    } 
    @Test 
    public void testActivity() { 
     assertThat(activity).isNotNull(); 
    } 
    @Test 
    public void testMap() { 
     fragment = (SupportMapFragment)activity.getSupportFragmentManager().findFragmentById(R.id.map); 
     assertThat(fragment).isNotNull(); 
     ((SupportMapFragment)fragment).getMapAsync(new OnMapReadyCallback(){ 

      @Override 
      public void onMapReady(GoogleMap map) { 
       org.junit.Assert.assertTrue(map!=null);    
      } 
     }); 
    } 
    } 

所有測試現在是罰款以上版本8.1.0播放服務和播放 - 服務-地圖。

相關問題