从 ApplicationTest 启动时,DataBindingUtil 不能 return null
DataBindingUtil must not return null when launched from ApplicationTest
绑定在 运行 "ApplicationTest" 时为 NULL,但在 运行 "app" 时绑定是正确的。 无效。
应用测试
@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
}
输出:
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'
解决了问题...
绑定在 运行 "ApplicationTest" 时为 NULL,但在 运行 "app" 时绑定是正确的。
应用测试
@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
}
输出:
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'
解决了问题...