我正在将导航库应用于我的项目。
我得到这个错误:
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
将是第一个具有菜单导航的导航。
我该怎么办?
答案 0 :(得分:0)
编辑:如果是JAVA和NOT KOTLIN
在setContentView()
之后
添加
Fragment fragment = findViewById(R.id.nav_host_fragment);
基本上是初始化错误。
答案 1 :(得分:0)
AccountActivity:
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>