我在将 Robolectric 与包含 NavigationVIew 的 AppCompatActivity 一起使用时遇到了一些问题。
我发现了一些与此相关的帖子,但没有任何帮助
例如这个:Robolectric 3.0 not working with AppCompat 21+ or https://github.com/robolectric/robolectric/issues/1859
我的步骤:
我的毕业典礼:
compileSdkVersion 23
buildToolsVersion "23.0.2"
...
minSdkVersion 14
targetSdkVersion 23
...
testCompile 'junit:junit:4.12'
testCompile('org.robolectric:robolectric:3.0+') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
testCompile "org.robolectric:shadows-support-v4:3.0"
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.android.support:support-v4:23.2.0'
我添加了一个包含以下内容的 project.properties(还有一个 test-project.proerties):
android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.2.0
android.library.reference.2=../../build/intermediates/exploded-aar/com.android.support/support-v4/23.2.0
我的主要活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Other stuff (init toolbar etc..)
}
}
我的 MainActivityTest
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
public class MainActivityTest {
private MainActivity activity;
@Before
public void setup() {
// Convenience method to run MainActivity through the Activity Lifecycle methods:
// onCreate(...) => onStart() => onPostCreate(...) => onResume()
activity = Robolectric.setupActivity(MainActivity.class);
}
@Test
public void testLoadDefaultFragment() {
Fragment fragment = activity.getSupportFragmentManager()
.findFragmentByTag(HomeFragment.TAG);
assertTrue(fragment instanceof HomeFragment);
}
}
我仍然有这个错误:
android.view.InflateException: XML file build\intermediates\res\merged\app\debug\layout\activity_main.xml line #-1 (sorry, not yet implemented): Error inflating class android.support.design.widget.NavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
at com.***.***.Activities.MainActivity.onCreate(MainActivity.java:43)
...
MainActivity.java 中的第 43 行是:
setContentView(R.layout.activity_main);
这是我的 activity_main :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
注意:我尝试 Robolectric 3.0、3.0+、3.1-SNAPSHOT 来支持 SDK 23,但没有任何效果
谢谢你的帮助。