片段onCreateView:错误膨胀类片段

时间:2015-03-21 21:30:08

标签: android fragment google-maps-android-api-2 android-xml

我经常遇到这个错误。

    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类有什么问题。

fragment_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>

FragmentMapTheater.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 个答案:

答案 0 :(得分:1)

去阅读松散的手册NestedFragments。然后你会看到你

  当布局包含a时,

无法将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。

答案 1 :(得分:0)

由于你没有发布你的活动的布局,很难说出一些东西,但我认为可能存在问题。