我在使用含有NavigationVIew的AppCompatActivity的Robolectric时遇到了一些问题。
我发现了一些与之相关的帖子,但没有任何帮助我
例如:Robolectric 3.0 not working with AppCompat 21+ 或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支持,没有任何作用
感谢您的帮助。