将“context context”属性添加到适配器构造函数后,尝试将“getcontext()”参数传递给在片段中创建的适配器示例时,会发生此错误。完全不知道该怎么办,一些决定,我发现在这样没有帮助。我将非常感激任何建议。
错误文本:
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;
}
活动\u 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
1条答案
按热度按时间6ju8rftf1#
尝试在中移动适配器初始化
onCreate
方法context
在视图初始化之前不可用: