在我的activity_main.XML文件中,我有三个主要部分, 1)ActionBar(ToolBar) - 它在顶部, 2)FrameLayout - 在中间, 3)BottomBar(ToolBar)。
现在在FrameLayout中我想根据底部工具栏中的按钮选择添加和替换片段。在我的第一个片段中,我有谷歌地图,在我的第二和第三片段中,我有ListView。我还在替换/添加片段
时添加了这个片段ft.addToBackStack(null);
现在问题是,每当我通过单击底部栏按钮打开每个片段时,所有片段都完全正常工作。但是,如果我从第一个(谷歌地图)片段打开第二个或第三个片段,如果我再次尝试通过单击后退按钮返回到同一个谷歌地图片段它在Logcat中显示这些错误。
Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0e00d9, tag null, or parent id 0xffffffff wi
09-16 16:46:29.112 6985-6985/com.imnv.im026.goodmorning E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.imnv.im026.goodmorning, PID: 6985
android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
........................ com.imnv.im026.goodmorning.FragmentLocate.onCreateView(FragmentLocate.java:64)
我的地图片段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"
android:background="@android:color/white"
android:orientation="vertical">
<fragment
android:id="@+id/locateDriverMap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/locateCallIconLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<include
android:id="@+id/includedCallIcon"
layout="@layout/locate_frament_bottom_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
我的任何地图片段类都是......
public class FragmentLocate extends Fragment {
private PrefManager pref;
ur="example.com/---";
private GoogleMap mMap;
Activity mActivity;
private View view;
private String userId, driverId;
private static double latitude1, longitude1;
android.support.v4.app.FragmentManager fragmentManager;
GetDriverForCustomer getDriverId;
public FragmentLocate() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.mActivity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_locate, container, false);
getDriverId = new GetDriverForCustomer(getActivity());
driverId = getDriverId.getDriverProfileDetails();
pref = new PrefManager(getActivity());
userId = pref.getKeyUserid();
getDriverGeoLocation();
mMap = ((SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
mMap.setOnMyLocationChangeListener(myLocationChangeListener);
return view;
}
@Override
public void onStart() {
super.onStart();
}
private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(loc).icon(null).title("Your Location"));
if (mMap != null) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
}
};
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude1, longitude1)).title("Driver Location").icon(BitmapDescriptorFactory.fromResource(R.drawable.car_marker)));
LatLng coordinate = new LatLng(latitude1, longitude1);
CameraUpdate DriverLocation = CameraUpdateFactory
.newLatLngZoom(coordinate, 12);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(DriverLocation);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(coordinate)
.zoom(17)
.build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
/*@Override
public void onResume() {
super.onResume();
*//*setUpMapIfNeeded();
if (mMap == null) {
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}*//*
}*/
public void getDriverGeoLocation() {
/*driverId = pref.getKeyDriverid();
Log.d("driverId", driverId);*/
String fullUrl = locateURL + driverId;
Log.d("PrintFullURL", fullUrl);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
fullUrl, null, new Response.Listener<JSONObject>() {
private String jsonResponse = "";
@Override
public void onResponse(JSONObject response) {
Log.d("Activity Car Profile ", response.toString());
try {
JSONArray jcarArray = response.getJSONArray("getdriverlocation");
for (int i = 0; i < jcarArray.length(); i++) {
JSONObject driverLocation = jcarArray.getJSONObject(i);
latitude1 = Double.parseDouble(driverLocation.getString("latitude"));
longitude1 = Double.parseDouble(driverLocation.getString("longitude"));
Log.e("latitude & longitude", latitude1 + " " + longitude1);
}
mMap.addMarker(new MarkerOptions().position(new LatLng(latitude1, longitude1)).title("Marker").icon(BitmapDescriptorFactory.fromResource(R.drawable.car_marker)));
LatLng coordinate = new LatLng(latitude1, longitude1);
CameraUpdate DriverLocation = CameraUpdateFactory
.newLatLngZoom(coordinate, 12);
mMap.setMyLocationEnabled(true);
mMap.animateCamera(DriverLocation);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(coordinate)
.zoom(17)
.build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Activity Car Profile", "Error: " + error.toString());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
}
我的清单文件是......
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:name=".app.AppController"
android:allowBackup="true"
android:icon="@mipmap/gm_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_maps_key" />
<meta-data
android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
<activity
android:name=".ViewPagerScreen"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="string/login_bt" />
<activity
android:name=".DriverShiftActivity"
android:label="@string/title_activity_driver_shift"
android:uiOptions="splitActionBarWhenNarrow">
<meta-data
android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>
<activity
android:name=".BaseMenuActivity"
android:label="@string/title_activity_base_menu"
android:uiOptions="splitActionBarWhenNarrow">
<meta-data
android:name="android.support.UI_OPTIONS"
android:value="splitActionBarWhenNarrow" />
</activity>
<activity
android:name=".SignUp"
android:label="@string/title_activity_sign_up" />
<activity
android:name=".DiaglogBoxShiftDriver"
android:label="@string/title_activity_diaglog_box_shift_driver"
android:theme="@android:style/Theme.Dialog" />
<activity
android:name=".FuelDetails"
android:label="@string/title_activity_fuel_details" />
<activity
android:name=".MyProfile"
android:label="@string/title_activity_my_profile" />
<activity
android:name=".CarProfile"
android:label="@string/title_activity_car_profile" />
<activity
android:name=".CustomerProfile"
android:label="@string/title_activity_customer_profile" />
<activity
android:name=".ChatTimeLine"
android:label="@string/title_activity_chat_time_line" />
<activity
android:name=".CustomerActivity"
android:label="@string/title_activity_customer"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerEnquiryActivity"
android:label="@string/title_activity_customer_enquiry"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerEmergencyActivity"
android:label="@string/title_activity_customer_emergency"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerMyProfile"
android:label="@string/title_activity_customer_my_profile"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerDriverProfile"
android:label="@string/title_activity_customer_driver_profile"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerPlaceInDetail"
android:label="@string/title_activity_customer_place_in_detail"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerSelectedCategory"
android:label="@string/title_activity_customer_selected_category"
android:theme="@style/MyMaterialTheme" />
<activity
android:name=".CustomerChangePassword"
android:label="@string/title_activity_customer_forget_password"
android:theme="@style/MyMaterialTheme" />
</application>
</manifest>
我只是在尝试通过单击后退按钮返回片段时重复显示错误。谢谢。
答案 0 :(得分:0)
在oncreat
中尝试此代码 if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;