1

My app is crashing as soon as it starts. Im getting the following error:

android.view.InflateException: Binary XML file line #34: Error inflating class

I've looked at several other similar questions but none of the solutions work for me. Why is this happening? Line #34 in the error refers to the

<fragment>..</fragment> 

in mainActivity.xml

MainActivity.java

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;




public class MainActivity extends AppCompatActivity
{

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




}

public void FragmentOneClick(View v)
{
    ListFragment myFragment=new fragmentOne();
    FragmentManager fm= getSupportFragmentManager();//getFragmentManager();

    FragmentTransaction ft=fm.beginTransaction();
    ft.replace(R.id.fragment_switch,   myFragment );
    ft.commit();

}

public void FragmentTwoClick(View v)
{
    Fragment myFragment=new fragmentTwo();
    FragmentManager fm= getSupportFragmentManager();

    FragmentTransaction ft=fm.beginTransaction();
    ft.replace(R.id.fragment_switch,  myFragment);
    ft.commit();

}

}

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.fragment_example.MainActivity">

    <Button
        android:onClick="FragmentOneClick"
    android:text="Fragment1"
    android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="150dp"
        android:layout_weight="2"
    >
    </Button>

<Button
    android:text="Fragment2"
    android:id="@+id/button2"
    android:onClick="FragmentTwoClick"
    android:layout_height="wrap_content"
    android:layout_width="150dp"
    android:layout_alignTop="@+id/button1"
    android:layout_alignRight="@+id/fragment_switch"
    android:layout_alignEnd="@+id/fragment_switch">
    android:layout_weight="2"
</Button>

<fragment
    android:id="@+id/fragment_switch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp"
    android:layout_below="@+id/button1"
    android:name="com.example.nirvan.fragment_example.fragmentOne"
    tools:layout="@layout/fragment_fragment_one" />

</RelativeLayout>

FragmentOne.java

//import android.app.ListFragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
//import android.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.support.v4.app.ListFragment;

public class fragmentOne extends android.support.v4.app.ListFragment
{
String dates[]={"1","2","3","4","5","6","7","8","9"};




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View rootView;
    rootView=inflater.inflate(R.layout.fragment_fragment_one, container, false);



    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,dates);
    setListAdapter(adapter);
}
}

FragmentTwo.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class fragmentTwo extends Fragment
{



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_fragment_two, container, false);
}


}

FragmentOne.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nirvan.fragment_example.fragmentOne">

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

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list"
    android:layout_gravity="left|top" />


</FrameLayout>

fragmentTwo.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nirvan.fragment_example.fragmentTwo"
android:background="#548f47">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:id="@+id/textview2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="fragment 2" />






</FrameLayout>

Log

 FATAL EXCEPTION: main
                                                                                            Process: com.example.nirvan.fragment_example, PID: 26870
                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirvan.fragment_example/com.example.nirvan.fragment_example.MainActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:139)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5086)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                                  Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
                                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
                                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
                                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                     at com.example.nirvan.fragment_example.MainActivity.onCreate(MainActivity.java:25)
                                                                                     at android.app.Activity.performCreate(Activity.java:5248)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:139) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:136) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5086) 
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                                                     at dalvik.system.NativeStart.main(Native Method) 
                                                                                  Caused by: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
                                                                                     at android.app.ListFragment.ensureList(ListFragment.java:402)
1
  • 1
    Edit your post and include the complete stack trace of the exception. Also, please don't include all of your code. The exception stack trace should he;p narrow down which code snippets are relevant. Commented Dec 28, 2016 at 16:41

1 Answer 1

1

Replace your FragmentOne.xml with this

 <FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

   <ListView
      android:id="@android:id/list"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_gravity="left|top" />


</FrameLayout>

Here you just needed to change the id of ListView to this

 android:id="@android:id/list"
Sign up to request clarification or add additional context in comments.

2 Comments

It worked! Thanks. Can you explain why changing the code for ID works?
Because you have extended ListFragment in FragmentOne so it internally needs this @android:id/list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.