【问题标题】:DataBindingUtil must not return null when launched from ApplicationTest从 ApplicationTest 启动时,DataBindingUtil 不得返回 null
【发布时间】:2017-05-11 06:47:25
【问题描述】:

在运行“ApplicationTest”时绑定为 NULL,但在运行“app”时是正确的。 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
}

输出:

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)

【问题讨论】:

  • 你的 gradle 和运行配置中有 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 吗?
  • 是的,我的 build.gradle 中有它。
  • 您是否尝试过直接从测试中启动 LoginActivity。它是否也返回一个空值?
  • 是的,它确实返回 null。但是,它曾经可以工作,我对直接启动 LoginActivity 不感兴趣
  • 你能发布你完整的onCreate()方法吗?

标签: android data-binding android-espresso android-databinding android-uiautomator


【解决方案1】:

更新

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

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

解决了问题...

【讨论】:

  • 我遇到了同样的问题,我没有使用retrolambda。您确定这是修复它还是可能是其他问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 2012-09-16
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
  • 2017-05-10
相关资源
最近更新 更多