Error Inflating Class Fragment

Error inflating class fragment because of an XML file

First of all, change the <fragment tag in you activity_main2.xml file to a FrameLayout because using a FrameLayout is necessary for switching between Fragments. Then use this code in your MainActivity java class:

public class MainActivity extends AppCompatActivity  {

//This will be used to switch between Fragments
private static FragmentManager fragmentManager;
private BottomNavigationView bottomNavView;


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

//Initialize the FragmentManager
fragmentManager = getSupportFragmentManager();

//Initialize the BottomNavigationView and add listener to it
bottomNavView = findViewById(R.id.bottom_navigation);
bottomNavView.setOnNavigationItemSelectedListener(bottomNavListener);
}

private BottomNavigationView.OnNavigationItemSelectedListener bottomNavListener =
new BottomNavigationView.OnNavigationItemSelectedListener(){

@Override
public boolean OnNavigationItemSelected(@NonNull MenuItem item){

Fragment selectedFragment = null;

switch(item.getItemId()){

case R.id.{first fragment}:
selectedFragment = new {first fragment}();
break;

case R.id.{second fragment}:
selectedFragment = new {second fragment}();
break;

.
.
.

{Make a case for each of your fragments}
}

fragmentManager.beginTransaction().replace(R.id.{id of your FrameLayout}, selectedFragment).commit();

}
};
}

This should work now.

android.view.InflateException: Binary XML file: Error inflating class fragment

After long time for debugging, I have fixed this problem. (Although I still cannot explain why). That I change property android:name to class. (although on Android Document, they say those properties are same, but it works !!!)

So, it should change from :

 android:name="com.fragment.NavigationDrawerFragment"

to

class = "com.fragment.NavigationDrawerFragment"

So, new layout should be :

<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->

<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
class = "com.fragment.NavigationDrawerFragment" />

Hope this help :)

Working With Fragments: `Error inflating class fragment`

Your fragment code onCreateView() should first inflate the list_fragment layout that contains the ListView. Then you should be calling findViewById() on the inflated layout and not on the activity since the fragment layout is not yet a part of the activity view hierarchy.

For example:

// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.list_fragment, container, false);
listView = (ListView) view.findViewById(R.id.testListView);
listView.setAdapter(adapter);

return view;

Binary XML file line #1: Error inflating class fragment MapFragment

Instead of this

 <fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="200dp"
android:name="com.google.android.gms.maps.MapFragment"/>

use this

 <fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
/>

in java class

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ClinicFragment extends Fragment implements OnMapReadyCallback {

private GoogleMap mMap;

public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);

SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

return view;
}


/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}

Error inflating class Fragment, when adding navGraph attribute

Your navigation graph needs to have at least one destination - the start destination of your graph and the first screen users see when you inflate your graph as per the Getting Started guide.

Binary XML file line #20: Error inflating class fragment

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8 at java.util.Arrays$ArrayList.get(Arrays.java:3769) at me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)
Here's the real reason for the crash ^

It seems to me that your

for(int i =0;i<9;i++){
addElement(uris.get(i),names.get(i));
}

tries to get an item that is out of bounds.
It should be i<8, because while your list seems to have 8 entries (length = 8 in the exception), they start at 0 (0,1,2...6,7 for a total of 8 entries), so i=8 and i=9 will be out of bounds.

You could also set i<uris.size(), that should also prevent crashing if the length of your uris and names lists are the same

Binary XML file line #11: Error inflating class fragment

For both of your fragments, you are not telling it how to create a view. I see that you are using the tools:layout tag, but according to the Tools doc, that is only a hint to the designer; it does not actually inflate that layout:

"This attribute is typically set in a tag and is used to record which layout you want to see rendered at designtime (at runtime, this will be determined by the actions of the fragment class listed by the tag)."

Thus you need to override onCreateView, inflate your view hierarchy, and then return that:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.topfragment, container, false);
}


Related Topics



Leave a reply



Submit