Espresso test fails with java.lang.String cannot b

2019-09-10 11:03发布

问题:

I get this error when my espresso test runs with android databinding on the fragment layout. I dont see any crash when I use the app. Is there anyway I can avoid this?

onCreateView method in fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    SponsorDetailBinding mBinding = DataBindingUtil.inflate(inflater,     R.layout.fragment_sponsordetail, container, false);

    return mBinding.getRoot();
}

fragment_sponsordetail layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<data class="SponsorDetailBinding"/>

<LinearLayout
    android:id="@+id/topics"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/sponsor_detail_topics_top"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <TextView
        style="@style/SponsorDetailText.Headers"
        android:text="@string/topics_header"/>

</LinearLayout>

The stack trace is

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.abc.events.databinding.SponsorDetailBinding at com.abc.events.ui.detail.SponsorDetailFragment.onCreateView(SponsorDetailFragment.java:102) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:924) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1116) at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1218) at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2170) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:300) at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:838) at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34) at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:826) at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$WrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:280) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) ... 24 more