3

我的活动 xml 包含 customeView (TitleToolbar)。当我运行单元测试时,robolectric 在我的 Activity 创建的 onCreate 方法中失败了。异常详细信息说它未能创建 customeView(TitleToolbar) 并抛出“错误膨胀类”

错误详情:

android.view.InflateException: XML file /Users../layout/Myactivity_UI.xml line #-1 (对不起,尚未实现): Error inflating class com.sample.view.TitleToolbar .... 引起:java.lang .reflect.InvocationTargetException

这是我的xml代码:

 <LinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
         <com.sample.view.TitleToolbar
             android:id="@+id/header"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />
        <TextView.....
        <TextView..... 
</LinearLayout>

这是测试代码

 @RunWith(RobolectricTestRunner.class)
 @Config(constants = BuildConfig.class)
 public class SampleTest {

        @Before
        public void setUp() throws NoSuchFieldException, IllegalAccessException {
           ActivityController<MyActivity> activityController = Robolectric.buildActivity(MyActivity.class);
           MyActivity activity = activityController.get();
           activityController.setup();  // this will call onCreate method of activity
.......

这是活动 onCreate 代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {             
        super.onCreate(savedInstanceState); // this is where robolectric failed to create custom TitleToolbar and throw InflateExcepion. 
4

0 回答 0