Shadow icon indicating copy to clipboard operation
Shadow copied to clipboard

Databinding 无法调用到 Layout 上的控件

Open LeonWu6 opened this issue 2 years ago • 0 comments

这可能是跟 Shadow 框架不相关的异常,但由于研究多天找不到原因,跑来此论坛请教大佬!

问题描述: 我应用是导入 shadow 框架插件化改造过的应用。 在宿主 app 和 插件 app 分别都使能 dataBinding,但出现了在插件 app 无法使用 binding对象调用 layout上控件的异常。

  1. 我的工程目录:
projectRoot
 -app                //宿主app
 -buildSrc
 -host-lib
 -pluginmanager
 -splitApk                  // 这个目录下包含以下四个moudle
     -CellularApp        //插件app
     -CellularLoader
     -CellularRuntime
     -rfkitlib
 -utils
  1. 在所有 moudle 中使能 dataBinding和viewBinding
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'

android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}
  1. 在插件app中写一个databinding的layout
<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >
    <data>
        <variable name="testviewString" type="String" />
    </data>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       **<!--TextView 有绑定数据 -->**
        <TextView
            android:id="@+id/test_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@{testviewString}" />
        **<!--Button  无绑定数据 -->**
        <Button
            android:id="@+id/test_bt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</layout>
  1. Activity 的测试代码如下:
override fun onCreate(p0: Bundle?) {
        super.onCreate(p0)
        mLogger.debug("onCreate")
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_test)
        if (mBinding.testTv == null) {  **// mBinding.testTv 不会等于空**
            mLogger.error("onCreate mBinding.testTv == null")
        } else {
            mLogger.debug("onCreate mBinding.testTv != null")
        }
        if (mBinding.testBt == null) {  **// mBinding.testBt  等于空,无法用mBinding获得button控件**
            mLogger.error("onCreate mBinding.testBt == null")
        } else {
            mLogger.debug("onCreate mBinding.testBt != null")
        }
}

对应用的日志打印:

D  onCreate
D  onCreate mBinding.testTv != null
E  onCreate mBinding.testBt == null
  1. 总结描述: 在我的插件app CellularApp中,如果在dataBinding Layout中,一个没有绑定 variable的控件,无法用dataBinding的对象获取到。但在宿主app 中,没有这个异常,正常来说,无论一个控件有没有绑定变量,都应该可以用 dataBinding的对象获得。

请问您知道这是什么问题吗?

LeonWu6 avatar Oct 20 '23 01:10 LeonWu6