【问题标题】:InflateException while implementing Navigation Library实现导航库时出现 InflateException
【发布时间】:2019-05-20 04:02:08
【问题描述】:

我正在将导航库应用到我的项目中。

我得到这个错误: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment

这是MainActivity.kt

   val fragmentManager = supportFragmentManager

   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        if(savedInstanceState == null){
            fragmentManager.beginTransaction().add(R.id.nav_host_fragment, InitFragment()).commit()
        }else{

        }
    }

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:tint="#555"
        tools:context="com.example.view.main.MainActivity">

        <ImageView
            android:id="@+id/iv_flame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"/>
    </RelativeLayout>
</layout>

这是InitFragment.kt

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment

        var binding = DataBindingUtil.inflate<FragmentInitBinding>(inflater, R.layout.fragment_init, container, false)
        binding!!.initVm = InitViewModel(this@InitFragment)

        var view = binding.root
        return view
    }

这是fragment_init.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bind="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="initVm"
            type="com.example.vm.InitViewModel" />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/transparent"
        tools:context=".view.main.fragment.LoginFragment">

</layout>

我认为这段代码没有任何问题。我需要为此执行任何额外的工作吗?我正在使用数据绑定。但我认为MainActivity.kt 不需要。

MainActivity.kt 包含所有片段。而initFragment.kt 将是第一个具有导航菜单的导航。

我该怎么办?

【问题讨论】:

  • 我认为问题是——fragmentManager.beginTransaction().add(R.id.nav_host_fragment, InitFragment()).commit()——这一行。请删除此行并尝试。
  • @DeepakRajput 我评论了那部分,但发生了同样的错误。
  • 你到底想做什么??您只想使用导航权将片段放入活动中?
  • @DeepakRajput 是的..
  • 我在下面发布了一个演示。请检查一下。

标签: android android-fragments android-inflate android-navigation


【解决方案1】:

编辑:如果是 JAVA 而不是 KOTLIN 在setContentView()之后 添加

Fragment fragment = findViewById(R.id.nav_host_fragment);

基本上是初始化错误。

【讨论】:

  • 好吧,我觉得Fragment fragment = findViewById(R.id.nav_host_fragment) 没有意义?
  • 确实如此。抱歉,我以为是java。我会修改的!
【解决方案2】:

帐户活动:

class AccountActivity : AppCompatActivity(){
private lateinit var activityAccountBinding: ActivityAccountBinding


 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding = DataBindingUtil.setContentView<ViewDataBinding>(this, 
         R.layout.activity_account)
    setDataBinder(binding)
    setup()
}

override fun setDataBinder(viewDataBinding: ViewDataBinding) {
    activityAccountBinding = viewDataBinding as ActivityAccountBinding
    // to getting events in this file(Click event)
    activityAccountBinding.accountActivity = this
}

override fun setup() {

}

}

activity_account:-

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="accountActivity"

    type="com.app.presentation.myaccount.activity.AccountActivity" 
   />

</data>

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red_fd3d50">

    <fragment
            android:id="@+id/fragmentContainer"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/my_account_graph"/>



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

my_account_graph:-

 <?xml version="1.0" encoding="utf-8"?>
 <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_account_graph"
    app:startDestination="@id/profileFragment">

    <fragment
       android:id="@+id/profileFragment"
       android:name="com.app.presentation.myaccount.ProfileFragment"
       android:label="ProfileFragment"
       tools:layout="@layout/fragment_profile" />
 </navigation>

【讨论】:

  • 好吧,我就像你发布的那样实现了。但是没有用。可以分享一下示例项目吗?
  • 您面临什么样的问题?
  • 同样的问题。 android.view.InflateException。我不需要检查片段部分,对吗?我想检查一下这个项目。我正在遵循this 的指令及其回购。但我不知道如何正确构建项目。
  • 你说你只想要一个里面有一个片段的活动,因为你不需要任何导航代码。仅当您想从一个片段切换到另一个片段时才需要它。更多使用此链接:-journaldev.com/22299/android-jetpack-navigation-architecture
猜你喜欢
  • 2021-12-17
  • 2019-01-28
  • 2017-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多