【问题标题】:Can not resolve symbol Fragment XX impl无法解析符号 Fragment XX impl
【发布时间】:2020-01-08 10:19:33
【问题描述】:

我在mainActivity下有一个fragment,而mainActivity使用数据绑定包裹fragment:

// activity_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <FrameLayout
        android:id="@+id/loginFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</layout>

LoginFragment 还使用与 LoginViewModel 的数据绑定:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="loginViewModel"
            type="com.example.LoginViewModel" />
    </data>

    ...
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

但是当我编译项目时,import com.example.dccandroidapp.FragmentLoginBindingImpl;这行,却报错说明绑定是假的。

【问题讨论】:

  • 添加完整的堆栈跟踪

标签: android kotlin data-binding


【解决方案1】:

您是否在 app/build.gradle 中启用了数据绑定?另外,尝试清理项目,然后重新构建它 - 如果您还没有完成这些步骤。

【讨论】:

    【解决方案2】:

    出现这个问题是因为我没有使用supportFragmentManager 来初始化活动中的片段。将此添加到 MainActivity 中的 onCreate。

            val currentFragment =
                supportFragmentManager.findFragmentById(R.id.fragment_container)
    
            if (currentFragment == null) {
                val fragment = LoginFragment()
                supportFragmentManager
                    .beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit()
            }
    

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 2021-02-04
      • 2016-12-31
      • 1970-01-01
      相关资源
      最近更新 更多