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 :)
Error: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class fragment
According to me you can try to inflate your fragment by other methods like this:
add container for fragment in main activity layout. you can simply replace with this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:layout_width="50dp"
android:layout_height="50dp" />
</RelativeLayout>
In code you can add in onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (FrameLayout) findViewById(R.id.container);
//set Fragment to activity
BlankFragment frag = new BlankFragment();
getFragmentManager().beginTransaction().add(R.id.container,frag).commit();
}
and also you face problem with list id you can refer this link
Hope this will help.
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 adding permission in manifest, It solved my issue. Check below code :
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="24" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="24" />
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.
Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment
Yes.. I want Map inside a Fragment.
You should use a MapView
http://developer.android.com/reference/com/google/android/gms/maps/MapView.html
public class HowToReach extends Fragment {
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
// Gets the MapView from the XML layout and creates it
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
Log.e("Address Map", "Could not initialize google play", e);
}
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
{
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if(mapView!=null)
{
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
map.animateCamera(cameraUpdate);
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default: Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
// Updates the location and zoom of the MapView
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</RelativeLayout>
Update:
https://developers.google.com/android/reference/com/google/android/gms/maps/MapView#public-constructors
getMap()
is deprecated. Use getMapAsync(OnMapReadyCallback)
instead. The callback method provides you with a GoogleMap instance guaranteed to be non-null and ready to be used.
android.view.InflateException: Binary XML file line #14: Error inflating class fragment
Try to remove your mapFragment on onDestroyView in LocationFragment.java.
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null)
getChildFragmentManager().beginTransaction().remove(mapFragment).commitAllowingStateLoss();
}
Binary XML file line #8: Error inflating class fragment Caused by: android.view.InflateException:
Problem was in attempt to get Views with help of getActivity().findViewById(...)
. I used code example based on Support Library which doesn't have getView()
method. Using getView()
solved problem.
Related Topics
Android Studio Keeps Refusing to Resolve Com.Android.Support:Appcompat-V7:29.0.1
Self-Signed Ssl Acceptance on Android
Android Studio Add External Project to Build.Gradle
How to Make Ellipsize="Marquee" Always Scroll
What's the Best Way to Iterate an Android Cursor
How to Implement Android Pull-To-Refresh
How to Remove Application from Recent Application List
How to Run a Method Every X Seconds
How to View the SQLite Database on an Android Device
Android View.Getdrawingcache Returns Null, Only Null
Animate the Transition Between Fragments
Clicking Urls Opens Default Browser
Using Facebook Sdk in Android Studio