gpt4 book ai didi

java - fragment 中的 map [IllegalArgumentException : Binary XML file line #7: Duplicate id]

转载 作者:行者123 更新时间:2023-11-29 08:48:15 25 4
gpt4 key购买 nike

所以,我有两个 fragment (F1F2),F1 包含 map fragmentF2 包含一些数据。所以当我加载 fragment F1 时, map 加载完美,但是当我转到 fragment F2 并通过单击按钮(再次加载 fragment )返回 fragment 一时,我收到以下错误:

06-03 16:13:56.227: E/AndroidRuntime(7748): FATAL EXCEPTION: main
06-03 16:13:56.227: E/AndroidRuntime(7748): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-03 16:13:56.227: E/AndroidRuntime(7748): at com.porcupyne.mumbaifoodlovers.MapsFragment.onCreateView(MapsFragment.java:46)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.Fragment.performCreateView(Fragment.java:1695)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.BackStackRecord.run(BackStackRecord.java:694)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.os.Handler.handleCallback(Handler.java:725)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.os.Looper.loop(Looper.java:153)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.ActivityThread.main(ActivityThread.java:5297)
06-03 16:13:56.227: E/AndroidRuntime(7748): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 16:13:56.227: E/AndroidRuntime(7748): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 16:13:56.227: E/AndroidRuntime(7748): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-03 16:13:56.227: E/AndroidRuntime(7748): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-03 16:13:56.227: E/AndroidRuntime(7748): at dalvik.system.NativeStart.main(Native Method)
06-03 16:13:56.227: E/AndroidRuntime(7748): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f08006a, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.app.Activity.onCreateView(Activity.java:4740)
06-03 16:13:56.227: E/AndroidRuntime(7748): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-03 16:13:56.227: E/AndroidRuntime(7748): ... 19 more

以下是我的 map fragment 代码(F1)MapsFragment.xml

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

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>

</RelativeLayout>

这是java代码:MapsFragment.java

public class MapsFragment extends Fragment {

private GoogleMap googleMap;
double latitude;
double longitude;
protected LocationManager locationManager;
Context context;
boolean isNetworkAvailable = false;
Location location;

public MapsFragment() {
}

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

View rootView = inflater.inflate(R.layout.fragment_maps, container,
false);

try {
initializeMap();

LatLng pos = new LatLng(latitude, longitude);
MarkerOptions marker = new MarkerOptions()
.position(pos)
.title("Fishork")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
MarkerOptions marker1 = new MarkerOptions()
.position(new LatLng(18.90370, 72.81314))
.title("Zaffran")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));

CircleOptions circle = new CircleOptions().center(pos).radius(2000)
.strokeColor(Color.parseColor("#FFFFFF")).strokeWidth(5)
.fillColor(Color.parseColor("#25f67f21"));

CircleOptions circle1 = new CircleOptions()
.center(new LatLng(18.90370, 72.81314)).radius(1000)
.strokeColor(Color.parseColor("#FFFFFF")).strokeWidth(5)
.fillColor(Color.parseColor("#25f67f21"));

googleMap.addMarker(marker);
googleMap.addMarker(marker1);
googleMap.addCircle(circle);
googleMap.addCircle(circle1);

CameraPosition cp = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));

googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);

} catch (Exception e) {
Log.e("TAG", "MSG: " + e);
}

return rootView;
}

private void initializeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();

if (googleMap == null) {
Toast.makeText(MapsFragment.this.getActivity(),
"Sorry! unable to create maps", Toast.LENGTH_LONG)
.show();
}
}
}

@Override
public void onDestroyView() {
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager()
.beginTransaction();
ft.remove(fragment);
ft.commit();
}
}

有人可以帮忙吗?!谢谢。

最佳答案

public MapsFragment() {
}

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

//Let the mapFragment draw itself and then you return this view later at the bottom.
View mapView = super.onCreateView(inflater, container, savedInstanceState);


/*View rootView = inflater.inflate(R.layout.fragment_maps, container,
false);*/

try {
initializeMap();

LatLng pos = new LatLng(latitude, longitude);
MarkerOptions marker = new MarkerOptions()
.position(pos)
.title("Fishork")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));
MarkerOptions marker1 = new MarkerOptions()
.position(new LatLng(18.90370, 72.81314))
.title("Zaffran")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.marker));

CircleOptions circle = new CircleOptions().center(pos).radius(2000)
.strokeColor(Color.parseColor("#FFFFFF")).strokeWidth(5)
.fillColor(Color.parseColor("#25f67f21"));

CircleOptions circle1 = new CircleOptions()
.center(new LatLng(18.90370, 72.81314)).radius(1000)
.strokeColor(Color.parseColor("#FFFFFF")).strokeWidth(5)
.fillColor(Color.parseColor("#25f67f21"));

//Get instance to this googleMap
googleMap = this.getMap();


googleMap.addMarker(marker);
googleMap.addMarker(marker1);
googleMap.addCircle(circle);
googleMap.addCircle(circle1);

CameraPosition cp = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(10).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));

googleMap.setMyLocationEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);

} catch (Exception e) {
Log.e("TAG", "MSG: " + e);
}

return mapView;
}

关于java - fragment 中的 map [IllegalArgumentException : Binary XML file line #7: Duplicate id],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24013375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com