Android.View.Inflateexception: Binary Xml File: Error Inflating Class Fragment

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 :


<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />




<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 :)

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.

Handle android.view.InflateException: Error inflating class fragment

The error came about because I had not initialized lateinit var adapter: MainAdapter

I initialized this inside onCreateView() and the app did not crash.

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.

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);
}

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

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

Finally,I reviewed all code and I used the androidx library. I had the supportv4 but with androidx the app will run perfectly.

I changed all layouts of my app with the androidx components



Related Topics



Leave a reply



Submit