Android谷歌地图首次打开时加载,然后在第二次打开时崩溃

时间:2014-05-15 16:10:33

标签: android google-maps android-fragments android-nested-fragment

我在我的android项目中用片段实现了谷歌地图。当我第一次调用异步任务时加载片段并加载标记并显示correctley。

但是如果我移动到另一个片段然后再回到地图,它会在尝试再次加载时崩溃。

我的错误是:

android.view.InflateException: Binary XML file line #4: Error inflating class fragment
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
       at android.view.LayoutInflater.inflate(LayoutInflater.java)
       at android.view.LayoutInflater.inflate(LayoutInflater.java)
       at com.beerportfolio.beerportfoliopro.BreweryMap.onCreateView(BreweryMap.java:28)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
       at android.os.Handler.handleCallback(Handler.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #4: Duplicate id 0x7f09003f, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
       at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
       at android.view.LayoutInflater.inflate(LayoutInflater.java)
       at android.view.LayoutInflater.inflate(LayoutInflater.java)
       at com.beerportfolio.beerportfoliopro.BreweryMap.onCreateView(BreweryMap.java:28)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
       at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
       at android.os.Handler.handleCallback(Handler.java)
       at android.os.Handler.dispatchMessage(Handler.java)
       at android.os.Looper.loop(Looper.java)
       at android.app.ActivityThread.main(ActivityThread.java)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
       at dalvik.system.NativeStart.main(NativeStart.java)

在错误中它说了一些关于重复的内容,所以我认为重新加载地图标记时遇到了问题,因此我尝试在开始时放置一个明确的标记,但这并不能解决问题。

我的地图代码是:

public class BreweryMap extends Fragment {

    public BreweryMap(){}

    String beerId = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_brewmap, container, false);
        setHasOptionsMenu(true);

        //get user information
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);


        GoogleMap mMap;
        mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        mMap.clear();

        //add url
        String url = "myURL";

        //call async to get breweries to add to
        new GetVisitedBreweries(getActivity(), mMap).execute(url);


        return rootView;
    }

异步任务获取JSON解析信息并将地图标记加载到地图上。

3 个答案:

答案 0 :(得分:3)

link开始..检查..

以编程方式声明片段,而不是XML。这可能意味着嵌套布局。

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

<!-- Lots of fancy layout -->   

<RelativeLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RelativeLayout>
</RelativeLayout>

然后,您需要以编程方式创建片段,但需要仔细考虑片段生命周期(http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager())。为确保在正确的时间创建所有内容,您的代码应如下所示。

public class MyFragment extends Fragment {

private SupportMapFragment fragment;
private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.layout_with_map, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map);
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map, fragment).commit();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (map == null) {
        map = fragment.getMap();
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
    }
}
}

答案 1 :(得分:0)

我最近遇到了一个几乎相同的问题,下面的代码是我的解决方案。您应该能够重命名并添加额外的代码以执行您想要执行的操作。

public class TrackerFragment extends Fragment {

private SupportMapFragment mMap;
private GoogleMap googleMap;
//private boolean chronoRunning = false;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag3_action, container, false);

    activityBody();
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    mMap = (SupportMapFragment) fm.findFragmentById(R.id.map);
    if (mMap == null) {
        mMap = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.map, mMap).commit();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (googleMap == null) {
        googleMap = mMap.getMap();
        googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
    }
}

答案 2 :(得分:0)

我来不及了,但经过大量的搜索后,这种方法很有效。

以编程方式执行,在xml中使用普通的FrameLayout。

SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction =
            getChildFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.map, mapFragment);
    fragmentTransaction.commit();
    mapFragment.getMapAsync(this);