いくつかの断片があるナビゲーションドロワーがあります。各フラグメントには、レイアウトにMapFragmentがあります。最初は、アプリの実行時にナビゲーションドロワーをクリックするとフラグメントが一度開きますが、同じナビゲーションアイテムをクリックして同じフラグメントをもう一度開くと(他のフラグメントにアクセスした後)、onCreateView()
で次のエラーが発生します。
Binary XML file line #1: Error inflating class fragment
以下はフラグメントの私のコードです:
user_map_Fragment.xml
<LinearLayout
Android:layout_height="wrap_content"
Android:layout_width="match_parent"
Android:orientation="vertical"
Android:id="@+id/parentContainer"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:name="com.google.Android.gms.maps.MapFragment"/>
......
</LinearLayout>
MainFragment.cs
public class MainFragement : Android.Support.V4.App.Fragment, IOnMapReadyCallback, ILocationListener, Android.Views.View.IOnKeyListener
{
private GoogleMap mMap;
private LocationManager _locationManager;
private string _locationProvider;
private Android.Locations.Location _currentLocation;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (v == null)
{
v = inflater.Inflate(Resource.Layout.Main, container, false);
SetUpMap();
InitializeLocationManager();
}
return v;
}
}
private void SetUpMap()
{
if (mMap == null)
{
Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
}
}
クラッシュログ:
06-22 10:56:50.798 E/mono-rt (15470): Java.lang.IllegalArgumentException: Binary XML file line #1: Duplicate id 0x7f0a0099, tag null, or parent id 0x7f0a007b with another fragment for com.google.Android.gms.maps.MapFragment
06-22 10:56:50.798 E/mono-rt (15470): at Android.app.FragmentManagerImpl.onCreateView(FragmentManager.Java:2136)
06-22 10:56:50.798 E/mono-rt (15470): at Android.app.Activity.onCreateView(Activity.Java:5516)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.Java:34)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.Java:78)
06-22 10:56:50.798 E/mono-rt (15470): at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:733)
06-22 10:56:50.798 E/mono-rt (15470): at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:806)
06-22 10:56:50.798 E/mono-rt (15470): at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:809)
06-22 10:56:50.798 E/mono-rt (15470): at Android.view.LayoutInflater.inflate(LayoutInflater.Java:504)
06-22 10:56:50.798 E/mono-rt (15470): at Android.view.LayoutInflater.inflate(LayoutInflater.Java:414)
06-22 10:56:50.798 E/mono-rt (15470): at md5ec4b17f0066095dde498afe2bdaa8cb9.PeopleCheckinFragment.n_onCreateView(Native Method)
06-22 10:56:50.798 E/mono-rt (15470): at md5ec4b17f0066095dde498afe2bdaa8cb9.PeopleCheckinFragment.onCreateView(PeopleCheckinFragment.Java:39)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.Fragment.performCreateView(Fragment.Java:1962)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1026)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1207)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.BackStackRecord.run(BackStackRecord.Java:738)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.Java:1572)
06-22 10:56:50.798 E/mono-rt (15470): at Android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.Java:493)
06-22 10:56:50.798 E/mono-rt (15470): at Android.os.Handler.handleCallback(Handler.Java:739)
06-22 10:56:50.798 E/mono-rt (15470): at Android.os.Handler.dispatchMessage(Handler.Java:95)
06-22 10:56:50.798 E/mono-rt (15470): at Android.os.Looper.loop(Looper.Java:135)
06-22 10:56:50.798 E/mono-rt (15470): at Android.app.ActivityThread.main(ActivityThread.Java:5930)
06-22 10:56:50.798 E/mono-rt (15470): at Java.lang.reflect.Method.invoke(Native Method)
06-22 10:56:50.798 E/mono-rt (15470): at Java.lang.reflect.Method.invoke(Method.Java:372)
06-22 10:56:50.798 E/mono-rt (15470): at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1405)
06-22 10:56:50.798 E/mono-rt (15470): at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1200)
どうすればこれを解決できますか?
これの代わりに
<fragment
Android:id="@+id/map"
Android:layout_width="match_parent"
Android:layout_height="200dp"
Android:name="com.google.Android.gms.maps.MapFragment"/>
これを使って
<fragment
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="200dp"
/>
in Javaクラス
import Android.os.Bundle;
import Android.support.annotation.Nullable;
import Android.support.v4.app.Fragment;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
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.MarkerOptions;
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
変更してみてください
Android:name="com.google.Android.gms.maps.SupportMapFragment"
//and
Activity.FragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map).GetMapAsync(this);
の代わりに
Android:name="com.google.Android.gms.maps.MapFragment"
//and
Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
編集1
android.app.Fragmentを削除し、サポートパッケージをインポートします
import Android.support.v4.app.Fragment;
import Android.support.v4.app.FragmentActivity;
import Android.support.v4.app.FragmentManager;
編集2
これを試して
SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map));
これはあなたを助けるかもしれません
Android:hardwareAccelerated="true"
to my MapsActivity
on AndroidManifest.xml
私のために働いた。
私の場合、SupportMapFragmentを使用していても、GoogleマップにAPI_KEYを追加するのを忘れていました。
<fragment
Android:id="@+id/map"
Android:name="com.google.Android.gms.maps.SupportMapFragment"
Android:layout_width="match_parent"
Android:layout_height="200dp" />
マニフェスト:
<meta-data
Android:name="com.google.Android.geo.API_KEY"
Android:value="@string/google_maps_key" />