mypackage 中的二进制 XML 文件第 18 行:layout/activity_main:膨胀 class androidx.fragment.app.FragmentContainerView 时出错
Binary XML file line #18 in mypackage:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
当我将“Context context”属性 添加到适配器构造函数后,尝试将“getContext()”参数传递到我在 Fragment 中创建的适配器实例时,会发生此错误。
完全不知道该怎么做,我在 SO 中发现的一些决定没有帮助。
我将非常感谢任何建议。
错误文本:
java.lang.RuntimeException: Unable to start activity ComponentInfo{space.rodionov.swedishdriller/space.rodionov.swedishdriller.MainActivity}: android.view.InflateException: Binary XML file line #18 in space.rodionov.swedishdriller:layout/activity_main: Binary XML file line #18 in space.rodionov.swedishdriller:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
片段中:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
private var drillerAdapter = JavaDrillerAdapter(JavaDrillerAdapter.JavaDrillerDiff(), false, requireContext())
在适配器中:
public class JavaDrillerAdapter extends ListAdapter<Word, JavaDrillerAdapter.JavaDrillerViewHolder> {
private Boolean nativToForeign;
Context context;
protected JavaDrillerAdapter(@NonNull DiffUtil.ItemCallback diffCallback, Boolean nativToForeign, Context context) {
super(diffCallback);
this.nativToForeign = nativToForeign;
this.context = context;
}
activity_main.xml:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
//blablabla
/>
<androidx.fragment.app.FragmentContainerView //THIS IS THE LINE #18 THE ERROR REFERS TO
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_nav"
android:layout_below="@id/toolbar"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
//blablabla
/>
</RelativeLayout>
在MainActivity.kt中:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // THIS IS ALSO THE LINE MENTIONED IN THE ERROR
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.findNavController()
// and so on
尝试在 onCreate
方法中移动您的适配器初始化,context
在视图初始化之前不可用:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
private var drillerAdapter: JavaDrillerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
drillerAdapter = JavaDrillerAdapter(JavaDrillerAdapter.JavaDrillerDiff(), false, requireContext())
}
当我将“Context context”属性 添加到适配器构造函数后,尝试将“getContext()”参数传递到我在 Fragment 中创建的适配器实例时,会发生此错误。 完全不知道该怎么做,我在 SO 中发现的一些决定没有帮助。 我将非常感谢任何建议。
错误文本:
java.lang.RuntimeException: Unable to start activity ComponentInfo{space.rodionov.swedishdriller/space.rodionov.swedishdriller.MainActivity}: android.view.InflateException: Binary XML file line #18 in space.rodionov.swedishdriller:layout/activity_main: Binary XML file line #18 in space.rodionov.swedishdriller:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
片段中:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
private var drillerAdapter = JavaDrillerAdapter(JavaDrillerAdapter.JavaDrillerDiff(), false, requireContext())
在适配器中:
public class JavaDrillerAdapter extends ListAdapter<Word, JavaDrillerAdapter.JavaDrillerViewHolder> {
private Boolean nativToForeign;
Context context;
protected JavaDrillerAdapter(@NonNull DiffUtil.ItemCallback diffCallback, Boolean nativToForeign, Context context) {
super(diffCallback);
this.nativToForeign = nativToForeign;
this.context = context;
}
activity_main.xml:
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
//blablabla
/>
<androidx.fragment.app.FragmentContainerView //THIS IS THE LINE #18 THE ERROR REFERS TO
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_nav"
android:layout_below="@id/toolbar"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
//blablabla
/>
</RelativeLayout>
在MainActivity.kt中:
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) // THIS IS ALSO THE LINE MENTIONED IN THE ERROR
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.findNavController()
// and so on
尝试在 onCreate
方法中移动您的适配器初始化,context
在视图初始化之前不可用:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
private var drillerAdapter: JavaDrillerAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
drillerAdapter = JavaDrillerAdapter(JavaDrillerAdapter.JavaDrillerDiff(), false, requireContext())
}