I’m getting the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pstiming.mc/com.pstiming.mc.MainActivity}: android.view.InflateException: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
however activity_main.xml is
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id=“@+id/fragment_container”
android:name=“com.pstiming.mc.WaterPoloFragment”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“.MainActivity” />
which is copied from the book with the exception of the changed name. Line 10 is the tools:context line which is the same as the book. MainActivity.kt is the same as the book.
Can you share the Kotlin and XML code for WaterPoloFragment
? There might be a bug in one of those files.
When I try to post the XML for the WaterPoloFragment, I get the following error:
An error occurred: Sorry, new users can only put 2 links in a post.
package com.pstiming.mc
// WaterPoloFragment.kt
import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.pstiming.mc.databinding.FragmentWaterPoloBinding
class WaterPoloFragment : Fragment() {
private lateinit var binding : FragmentWaterPoloBinding
companion object {
fun newInstance() = WaterPoloFragment()
}
private lateinit var viewModel: WaterPoloViewModel
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
binding = FragmentWaterPoloBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated( view: View, savedInstanceState: Bundle? ) {
super.onViewCreated(view, savedInstanceState)
// binding.ivAwayIncScore.setOnClickListener {
// binding.tvAwayScore.text = (binding.tvAwayScore.text.toString().toInt() + 1).toString()
// }
}
}
Use the </>
button when typing up your message. That will wrap your XML code in styling specific for big chunks of code.
For example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline5"
android:text="@string/crime_title_label" />
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/crime_title_hint"
android:importantForAutofill="no"
android:inputType="text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceHeadline5"
android:text="@string/crime_details_label" />
<Button
android:id="@+id/crime_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Wed May 11 11:56 EST 2022" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".WaterPoloFragment"
android:id="@+id/contraint_layout_wp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/away_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
That all looks fine to me. Any chance you could post the full stacktrace for the crash? There might be a line with a “Caused by:” message that could really help us narrow down the issue.
FATAL EXCEPTION: main
Process: com.pstiming.mc, PID: 23833
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pstiming.mc/com.pstiming.mc.MainActivity}: android.view.InflateException: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.view.InflateException: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #10 in com.pstiming.mc:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:858)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1010)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:965)
at android.view.LayoutInflater.inflate(LayoutInflater.java:663)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:461)
at android.app.Activity.setContentView(Activity.java:3523)
at androidx.activity.ComponentActivity.setContentView(ComponentActivity.java:465)
at com.pstiming.mc.MainActivity.onCreate(MainActivity.kt:41)
at android.app.Activity.performCreate(Activity.java:8054)
at android.app.Activity.performCreate(Activity.java:8034)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1341)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3666)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3842)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2252)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7842)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.UnsupportedOperationException: FragmentContainerView must be within a FragmentActivity to use android:name="com.pstiming.mc.WaterPoloFragment"
at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.kt:120)
Ah, here’s the error. What class does your MainActivity
extend? In the book we always use AppCompatActivity
.