Error inflating class Fragment when testing fragment using DataBinding

安稳与你 提交于 2020-01-04 06:39:08

问题


Can someone help me on this? In my test I want to check if the login button is displayed on the StartPage. I've been struggling with this error for a while today and I don't know how it should be resolved.What am I missing here?

Maybe this problem is somehow similar to this question: Espresso test fails with java.lang.String cannot be cast to com.abc.events.databinding.SponsorDetailBinding

android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at io.turbela.turbela.login.view.StartActivity.onCreate(StartActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:532)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void io.turbela.turbela.databinding.FragmentStartBinding.setViewModel(io.turbela.turbela.login.viewmodel.DispatchViewModel)' on a null object reference
at io.turbela.turbela.StartActivityFragment.onCreateView(StartActivityFragment.java:34)
at android.app.Fragment.performCreateView(Fragment.java:2114)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1060)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1162)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2131)
at android.app.Activity.onCreateView(Activity.java:5610)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:740)
... 20 more

LoginTest.java

@RunWith(AndroidJUnit4.class)
public class LoginTest {

@Rule
public ActivityTestRule<StartActivity> mActivityRule = new ActivityTestRule(StartActivity.class);

@Test
public void displayLoginPage() {
    onView(withId(R.id.login_button)).check(matches(isDisplayed()));
}
}

StartActivity.java

public class StartActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);
}
 ...onCreateOptionsMenu(...
 ...onOptionsItemSelected(...
}

activity_start.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
class="io.turbela.turbela.StartActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

StartActivityFragment.java

public class StartActivityFragment extends Fragment {

public StartActivityFragment() {
}

public static StartActivityFragment newInstance(){
    Bundle args = new Bundle();
    args.putString("key", "value");
    StartActivityFragment fragment = new StartActivityFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    FragmentStartBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_start, container, false);
    binding.setViewModel(new DispatchViewModel());
    return binding.getRoot();
}
}

fragment_start.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
    <variable name="viewModel" type="io.turbela.turbela.login.viewmodel.DispatchViewModel" />
</data>

<RelativeLayout
    style="@android:style/Theme.Holo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <Button
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="128dp"
        android:onClick="@{viewModel.onClickLogin}"
        android:text="@string/login" />
</RelativeLayout>
</layout>

DispatchViewModel.java

public class DispatchViewModel extends BaseObservable{


public View.OnClickListener onClickLogin() {
    //loginWithEmail
    return v -> Log.d("H", "onClickLogin");
}
}

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "io.turbela.turbela"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

dataBinding{
    enabled = true
}

packagingOptions {
    exclude 'LICENSE.txt'
}
lintOptions {
    abortOnError false
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:cardview-v7:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.parse:parse-android:1.12.0'
compile 'com.parse.bolts:bolts-android:1.3.0'
compile 'com.facebook.android:facebook-android-sdk:4.8.1'
compile 'com.parse:parsefacebookutils-v4-android:1.10.3@aar'


androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'

}

回答1:


There is android:name attribute missing from <fragment> tag in your activity_start.xml

As per the docs, you must include it.

So your activity.start.xml should look like this:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="io.turbela.turbela.StartActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Note:

The class= attribute you are trying to use is for custom Views as in here



来源:https://stackoverflow.com/questions/36517853/error-inflating-class-fragment-when-testing-fragment-using-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!