3

運行「ApplicationTest」時綁定爲NULL,但在運行「app」時正確。 This post不起作用。從ApplicationTest啓動時,DataBindingUtil不得返回null

ApplicationTest

@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("[email protected]")); 

    } 
} 

LoginActivity

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:所遇到的異常:[email protected]。將線程狀態轉儲爲輸出並尋找峽灣。 java.lang.NullPointerException:嘗試從字段'android.widget.VideoView nl.brandmkrs.damageapp.databinding.LoginActivityBinding.videoView'讀取空對象引用 at nl.brandmkrs.damageapp.view.LoginActivity.onCreate(LoginActivity的.java:96)

+0

你有'testInstrumentationRunner「android.support.test.runner.AndroidJUnitRunner」'在你的gradle和運行配置嗎? –

+0

是的,我在我的build.gradle。 –

+0

您是否嘗試過直接從測試中啓動LoginActivity?它是否也返回null? – AndroidGuy

回答

4

更新

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

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

問題解決了......

+0

我遇到同樣的問題,我沒有使用retrolambda。你確定這是否修復了它,或者它可能是其他的東西? – McFarlane

相關問題