Issue
This error occurs when I, after adding "Context context" property to the adapter constructor, try to pass "getContext()" argument to the adapter instance I create in the Fragment. Completely no idea what to do, some decisions I found in SO did not help. I will deeply appreciate any advice.
Error text:
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
In fragment:
@AndroidEntryPoint
class DrillerFragment : Fragment(R.layout.cardstack_layout), CardStackListener {
private val viewModel: DrillerViewModel by viewModels()
private var drillerAdapter = JavaDrillerAdapter(JavaDrillerAdapter.JavaDrillerDiff(), false, requireContext())
In adapter:
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>
In 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
Solution
Try to move your adapter initialization in the onCreate
method, the context
is unavailable before views initialization:
@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())
}
Answered By - Luca Pizzini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.