You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android InflateException错误:fragment类加载失败求助

Fixing "Error inflating class fragment: android.view.InflateException" in your MapFragment setup

Let's break down what's going wrong and fix this issue step by step:

Key Issues Identified

  • Your layout XML is trying to inflate the system's SupportMapFragment, but you've created a custom MapFragment class — this mismatch is causing the inflation error.
  • You're manually calling mapFragment.onCreateView() in your Activity, which violates Android's Fragment lifecycle rules (Fragment lifecycle methods should be managed by the system, not called directly).
  • Your custom MapFragment code is incomplete (the onCreateView method cuts off), which would also cause runtime issues.

Step 1: Fix the Layout XML

Update the <fragment> tag in your layout to point to your custom MapFragment class instead of the system one. Replace the class attribute with the full package path of your fragment, and use a responsive width instead of a fixed value:

<fragment
    android:id="@+id/map"
    class="homework.android.aut.bme.hu.shoppy.map.MapFragment" <!-- Updated class path -->
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:tag="map"/>

Step 2: Fix the EntryDetailActivity Code

Remove the manual Fragment creation and onCreateView call. Instead, retrieve the already-inflated MapFragment from the FragmentManager and pass your location arguments to it:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_entry_detail);
    ButterKnife.bind(this);
    
    Intent intent = getIntent();
    tmpEntry = (Entry) intent.getSerializableExtra("tmpEntry");
    DetailTitle.setText(tmpEntry.getTitle());
    DetailBody.setText(tmpEntry.getBody());

    // Get the existing MapFragment from the layout
    MapFragment mapFragment = (MapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    
    // Pass location arguments to the fragment
    Bundle args = new Bundle();
    args.putDouble("latitude", tmpEntry.getLatitude());
    args.putDouble("longitude", tmpEntry.getLongitude());
    mapFragment.setArguments(args);
}

Step 3: Complete the Custom MapFragment Class

Finish the onCreateView method, handle the arguments, and implement the map setup correctly:

package homework.android.aut.bme.hu.shoppy.map;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdate;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import homework.android.aut.bme.hu.shoppy.R;

public class MapFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap googleMap;
    private double latitude;
    private double longitude;
    private Marker marker;

    public MapFragment() {
        // Required empty public constructor
        this.latitude = 0;
        this.longitude = 0;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve arguments passed from the Activity
        if (getArguments() != null) {
            latitude = getArguments().getDouble("latitude");
            longitude = getArguments().getDouble("longitude");
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the fragment layout
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        
        // Get the SupportMapFragment from the fragment's layout and set the callback
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_container);
        if (mapFragment != null) {
            mapFragment.getMapAsync(this);
        }
        
        return view;
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;
        
        // Create a LatLng object with the passed coordinates
        LatLng location = new LatLng(latitude, longitude);
        
        // Add a marker and move the camera to the location
        marker = googleMap.addMarker(new MarkerOptions().position(location).title("Entry Location"));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 15);
        googleMap.moveCamera(cameraUpdate);
    }
}

Add a Layout for Your Custom MapFragment

Create a fragment_map.xml file for your custom fragment to hold the system map fragment:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</FrameLayout>

Why This Works

  • We now have a clear separation: your custom MapFragment wraps the system SupportMapFragment, which handles the actual map rendering.
  • The Fragment lifecycle is managed properly by Android, so inflation happens without conflicts.
  • Arguments are passed correctly from the Activity to the Fragment, ensuring the map uses the right location coordinates.

内容的提问来源于stack exchange,提问作者Kristof Rado

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:42:04