其他分享
首页 > 其他分享> > 从ApplicationTest启动时,DataBindingUtil不得返回null

从ApplicationTest启动时,DataBindingUtil不得返回null

作者:互联网

运行“ ApplicationTest”时,绑定为NULL,但运行“应用程序”时,绑定正确. This post不起作用.

应用测试

@RunWith(AndroidJUnit4.class)
public class ApplicationTest {

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

@Test
public void loginClickMarkerAndUploadDamageReport() {
    UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    //Some initialization code, and then LoginActivity gets started by the App

//This works..
onView(withId(R.id.user_email)).perform(replaceText("some@mail.com"));  

    }
}

登录活动

public class LoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final LoginActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
    }
}

activity_login

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

    <data>

        <variable
            name="dataSource"
            type="nl.brandmkrs.damageapp.model.User" />
    </data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:id="@+id/user_email"
        android:text="@{dataSource.password}"/>
</LinearLayout>
</layout>

build.gradle

defaultConfig { 
     minSdkVersion 17
     targetSdkVersion 25
     testInstrumentationRunner     
    "android.support.test.runner.AndroidJUnitRunner"
}
productFlavors {
    // The actual application flavor
    production {
        minSdkVersion 17
    }
    // Test application flavor for UIAutomator tests
    uiTest {
        minSdkVersion 18
    }
}
dataBinding {
    enabled = true
}

enter image description here

输出:

E / MonitoringInstrumentation:nl.brandmkrs.damageapp.view.LoginActivity@d8d4afd遇到异常.将线程状态转储到输出并固定到峡湾.
                                                              java.lang.NullPointerException:尝试从空对象引用中读取字段“ android.widget.VideoView nl.brandmkrs.damageapp.databinding.LoginActivityBinding.videoView”
                                                                  在nl.brandmkrs.damageapp.view.LoginActivity.onCreate(LoginActivity.java:96)

解决方法:

更新中

classpath 'me.tatarka:gradle-retrolambda:3.2.0'

classpath 'me.tatarka:gradle-retrolambda:3.4.0'

解决了问题…

标签:data-binding,android-espresso,android-databinding,android-uiautomator,android
来源: https://codeday.me/bug/20191026/1936254.html