Merrick Morris
Merrick Morris

Reputation: 3

java.lang.RuntimeException: Unable to start activity android.view.InflateException: Binary XML file line #13

I'm new at learning Android. I'll be short. I'm receiving the error as mentioned at the bottom. I've added a fragment via XML. I've searched for solutions but none worked so far. Where am I going wrong?

If I can be pointed to a solution that's okay. May I get an explanation as to why this happens?

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.workout, PID: 7653
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workout/com.example.workout.DetailActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2969)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:6861)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
 Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Must specify unique android:id, android:tag, or have a parent with an id for com.example.workout.WorkoutDetailFragment
    at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3177)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:791)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
    at com.example.workout.DetailActivity.onCreate(DetailActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:7110)
    at android.app.Activity.performCreate(Activity.java:7101)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2922)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:6861)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

DetailActivity Xml

<?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=".DetailActivity">

<fragment
    class="com.example.workout.WorkoutDetailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

DetailActivity Java

package com.example.workout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class DetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);
  }
}

Fragment xml

<!-- TODO: Update blank fragment layout -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textTitle"
        android:text="@string/workout_title"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textDescription"
        android:text="@string/workout_description"/>

</LinearLayout>

MainActivity Java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onShowDetails(View view){
    Intent intent = new Intent(this, DetailActivity.class);
    startActivity(intent);
 }
}

Upvotes: 0

Views: 5440

Answers (5)

Neel Doshi
Neel Doshi

Reputation: 1

This error is basically because you have not give id to your fragment container.

Before DetailActivity.xml

<?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=".DetailActivity">

<fragment
    class="com.example.workout.WorkoutDetailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

To Solve this give a unique identifier like this

After DetailActivity.xml

    <?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=".DetailActivity">
    
<fragment
     android:id="@+id/uniqueid" //this was missing
     class="com.example.workout.WorkoutDetailFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
/>

Upvotes: 0

deepa kn
deepa kn

Reputation: 11

Please specify the unique id for your fragment and try.

<fragment
    android:id="@+id/fragId"
    class="com.example.workout.WorkoutDetailFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 />

Upvotes: -1

Mahesh Pandit
Mahesh Pandit

Reputation: 358

Modify your DetailActivity.xml tag fragment as follow

  <fragment android:name="com.example.workout.WorkoutDetailFragment"
    android:id="@+id/frag"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Upvotes: 0

Praveen
Praveen

Reputation: 458

Implement your main activity with OnFragmentInteractionListener and change xml as per below code

public class MainActivity extends AppCompatActivity implements WorkoutDetailFragment.OnFragmentInteractionListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_activity);
}

@Override
public void onFragmentInteraction(Uri uri) {

}}

And your XML

<?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=".MainActivity">

<fragment
    android:id="@+id/fragment"
    android:name="com.example.myapplication.WorkoutDetailFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Share Fragment class file if you still can't execute

Upvotes: 0

SebastienRieu
SebastienRieu

Reputation: 1512

fix your detail_activity.xml replace class by android:name

<?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=".DetailActivity">

    <fragment
        android:name="com.example.workout.WorkoutDetailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 2

Related Questions