安卓,谷歌地图碎片和viewpager - 错误充气类片段碎片、片段、错误、地图

2023-09-05 02:20:55 作者:爱是用心的么。

我试图在一个片段中viewpager使用谷歌地图V2(fragmentpager)。它工作正常,如果我刷到第四片段(查看​​传呼机),但如果我回到第一个片段,然后回到第四 - 与地图 - 我的应用程序死

  07-04 19:38:20.937:E / AndroidRuntime(20175):致命异常:主要
07-04 19:38:20.937:E / AndroidRuntime(20175):android.view.InflateException:二进制XML
文件行#13:错误充气类片段
...
07-04 19:38:20.937:E / AndroidRuntime(20175):产生的原因:
java.lang.IllegalArgumentException:如果二进制XML文件中的行#13:重复ID 0x7f050010,
 标记为空,或与其他片段com.google.android.gms.maps.SupportMapFragment父ID为0x0

07-04 19:38:20.937:E / AndroidRuntime(20175):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-04 19:38:20.937:E / AndroidRuntime(20175):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
 

Android的Java的:

 公共类LocationDetail扩展片段{

私人SupportMapFragment mMapFragment;
私人GoogleMap的地图;
私人EventAdapter listAdapter;

保护MainActivity活动;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

    查看VW = inflater.inflate(R.layout.map,集装箱,假);

    尝试 {
        MapsInitializer.initialize(getActivity());
    }赶上(GooglePlayServicesNotAvailableException E){
        // TODO处理这种情况
    }

    地图=((SupportMapFragment)getFragmentManager()findFragmentById(R.id.map_container)。)的GetMap()。
    map.setMyLocationEnabled(真正的);


    map.addMarker(新MarkerOptions()
    .position(新经纬度(48.397141,9.98787))标题(Theatro乌尔姆俱乐部))。

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(新经纬度(48.397141,9.98787),14.0f));

    回归大众;
}

}
 

布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>

<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1.0
机器人:方向=垂直>

 <片段

    机器人:ID =@ + ID / map_container
    类=com.google.android.gms.maps.SupportMapFragment
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:名称=com.dr.diskoapp.MainActivity
    />
< / LinearLayout中>

<的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1.0
机器人:方向=垂直>

<的ListView
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:ID =@ + ID /列表>
< / ListView控件>
< / LinearLayout中>
< / LinearLayout中>
 
安卓7.1加入应用快捷方式 新机有压感屏

我已经看到有很多的线程有类似的问题,但没有解决办法: - /

perhapse任何人可以帮助这个时候。

----------编辑----- 我找到了解决办法:-)

 公共无效onDestroyView(){
    super.onDestroyView();

    尝试 {
        片段片段=(getFragmentManager()findFragmentById(R.id.map_container));
        FragmentTransaction英尺= getActivity()getSupportFragmentManager()的BeginTransaction()。
        ft.remove(片段);
        ft.commit();
    }赶上(例外五){
        e.printStackTrace();
    }
}
 

解决方案

 公共无效onDestroyView(){
super.onDestroyView();

尝试 {
    片段片段=(getFragmentManager()findFragmentById(R.id.map_container));
    FragmentTransaction英尺= getActivity()getSupportFragmentManager()的BeginTransaction()。
    ft.remove(片段);
    ft.commit();
}赶上(例外五){
    e.printStackTrace();
}
 

}

i'm trying to use google maps v2 within viewpager (fragmentpager) in a fragment. It works fine if i swipe to fourth fragment (view pager), but if i go back to the first fragment and then back to the fourth - with the map - my app dies.

07-04 19:38:20.937: E/AndroidRuntime(20175): FATAL EXCEPTION: main
07-04 19:38:20.937: E/AndroidRuntime(20175): android.view.InflateException: Binary XML     
file line #13: Error inflating class fragment
...
07-04 19:38:20.937: E/AndroidRuntime(20175): Caused by:     
java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f050010,
 tag null, or parent id 0x0 with another fragment for  com.google.android.gms.maps.SupportMapFragment

07-04 19:38:20.937: E/AndroidRuntime(20175):    at     android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-04 19:38:20.937: E/AndroidRuntime(20175):    at     android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)

Android Java:

public class LocationDetail extends Fragment {

private SupportMapFragment mMapFragment;
private GoogleMap map;
private EventAdapter listAdapter ;

protected MainActivity activity;

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

    View vw = inflater.inflate(R.layout.map, container, false);

    try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO handle this situation
    }

    map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_container)).getMap();
    map.setMyLocationEnabled(true);


    map.addMarker(new MarkerOptions()
    .position(new LatLng(48.397141, 9.98787)).title(" Theatro Club Ulm"));

    map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(48.397141, 9.98787) , 14.0f) );

    return vw;
}

}

Layout:

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

 <fragment

    android:id="@+id/map_container"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.dr.diskoapp.MainActivity"
    />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >

<ListView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list">          
</ListView>
</LinearLayout>
</LinearLayout>

I've seen that there are many threads with a similar problem, but no solution :-/

perhapse can anybody help this time.

---------- EDIT ----- I found the solution :-)

public void onDestroyView() {
    super.onDestroyView();

    try {
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_container));  
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方案

public void onDestroyView() {
super.onDestroyView();

try {
    Fragment fragment = (getFragmentManager().findFragmentById(R.id.map_container));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
} catch (Exception e) {
    e.printStackTrace();
}

}