代码之家  ›  专栏  ›  技术社区  ›  AG1

Fragment onCreateView:膨胀类片段时出错

  •  0
  • AG1  · 技术社区  · 10 年前

    我经常犯这个错误。

        android.view.InflateException: Binary XML file line #7: 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 com.txt2lrn.example.FragmentMapTheater.onCreateView(FragmentMapTheater.java:47)
    ...
     Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f08006d, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2174)
            at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:163)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            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 com.txt2lrn.example.FragmentMapTheater.onCreateView(FragmentMapTheater.java:47)
    

    不清楚我的*.xml文件或java类有什么问题。

    碎片_map_theater.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <fragment
            android:id="@+id/fragMap"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="200dp" />
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvTheaters"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/fragMap"
            android:background="@color/white"
            android:clickable="true"
            android:scrollbars="vertical" />
    
    </RelativeLayout>
    

    碎片地图剧院.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    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.UiSettings;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.JsonHttpResponseHandler;
    import com.loopj.android.http.RequestParams;
    
    import org.apache.http.Header;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    
    public class FragmentMapTheater extends Fragment implements OnMapReadyCallback {
        private final String TAG = getClass().getSimpleName().toString();
        private final int kGooglePlayServices = 1;
        private static final String mUrl = "http://goanuj.freeshell.org/foo.json";
        private ArrayList<ModelTheater> alTheaters;
        private RecyclerView mRV;
        private TheaterAdapter adapTheater;
        private SupportMapFragment mapFragment;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Log.d(TAG, "[onCreateView]");
            View v = inflater.inflate(R.layout.fragment_map_theater, container, false);
            //setHasOptionsMenu(true);
            //13:23 Android Project 6 Exploring TumblrSnap
            return v;
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.d(TAG, "[onActivityCreated]");
    
            int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());
            if (resultCode == ConnectionResult.SUCCESS) {
                Log.d(TAG, "G.Play AVAILABLE");
    
                // TODO: need to verify that this works with API 9
                // get mapfragment, since it is inside another fragment, call getChildFragmentManager()
                FragmentManager fm = getChildFragmentManager();
                mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.fragMap);
                mapFragment.getMapAsync(this);
    
                UiSettings ui = mapFragment.getMap().getUiSettings();
                ui.setCompassEnabled(false);
                ui.setZoomControlsEnabled(true);
                ui.setZoomGesturesEnabled(true);
    
                //if (!isNetworkAvailable()) {
                getData(10);
            } else {
                Log.d(TAG, "G.Play *NOT* AVAILABLE");
                GooglePlayServicesUtil.getErrorDialog(resultCode, getActivity(), kGooglePlayServices);
            }
    
            // setup RecyclerView
            mRV = (RecyclerView) getView().findViewById(R.id.rvTheaters);
            if (mRV == null) {
                Log.d(TAG, "mRV is null");
                return;
            }
    
            alTheaters = new ArrayList<ModelTheater>();
            adapTheater = new TheaterAdapter(alTheaters); // setup adapter
            mRV.setAdapter(adapTheater);
            mRV.setLayoutManager(new LinearLayoutManager(getActivity()));// setup LayoutManager
            mRV.setItemAnimator(new DefaultItemAnimator());// setup ItemAnimator
        }
    
        private void getData(int radius) {
            Log.d(TAG, "[getData] radius: " + radius + " miles");
            AsyncHttpClient client = new AsyncHttpClient();
            //RequestParams params = setupParams(mQuery,0);
            RequestParams params = null;
    
            final OnMapReadyCallback mapCall = this;
    
            client.get(mUrl, params, new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    Log.d("DEBUG", response.toString());
                    JSONArray jsonArrayTheaters = null;
                    try {
                        jsonArrayTheaters = response.getJSONArray("theaters");
                        // clear existing theaters from array (only from new search), add new ones
                        alTheaters.clear();
                        alTheaters.addAll(ModelTheater.fromJSONArray(jsonArrayTheaters));
                        Log.d(TAG, "[onSuccess]: " + alTheaters.size());
                        // for API 9, otherwise .addAll()
                        adapTheater.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    //Log.i("INFO", imageResults.toString());
                    //mRefreshView.setRefreshing(false);
    
                    mapFragment.getMapAsync(mapCall);
                }
    
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    //mRefreshView.setRefreshing(false);
                }
            } );
    
        }
    
        @Override
        public void onMapReady(GoogleMap map) {
            Log.d(TAG, "[onMapReady]");
            map.setMyLocationEnabled(true);
        }
    }
    
    2 回复  |  直到 10 年前
        1
  •  1
  •   Marian Paździoch    10 年前

    去读一下那本乱七八糟的手册 NestedFragments 。然后你会看到

    当布局包含时,无法将该布局膨胀为片段。仅当动态添加到片段时才支持嵌套片段。

        2
  •  0
  •   Kirill Popov    10 年前

    由于你们并没有发布你们的活动布局,所以很难说出什么,但我认为问题可能存在。