我在活动A,B,C中有3个片段
片段A
public class fragmentA extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a,container,false);
next = (TextView)view.findViewById(R.id.textView29);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fr = new fragmentB();
FragmentManager fm = getFragmentManager();
ragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}}
在片段B内,我将PlaceAutocompleteFragment与地图视图一起使用 xml
<fragment
android:id="@+id/place_autocomplete"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp">
<com.google.android.gms.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.google.android.gms.maps.MapView>
<!-- Image View that acts as map marker notice centerInParent-->
<ImageView
android:id="@+id/map_custom_marker"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerInParent="true"
android:src="@drawable/map_marker_new" />
</RelativeLayout>
片段B
public class fragmentB extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_b,container,false);
next = (TextView)view.findViewById(R.id.textView29);
MapView mMapView = (MapView) view.findViewById(R.id.mapView);
MapsInitializer.initialize(getActivity());
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
placeAutoComplete = (PlaceAutocompleteFragment)getChildFragmentManager().findFragmentById(R.id.place_autocomplete);
AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
placeAutoComplete.setFilter(typeFilter);
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final GoogleMap googleMap) {
LatLng posisiabsen;
posisiabsen = new LatLng(user_lat, user_lng); ////your lat lng
googleMap.moveCamera(CameraUpdateFactory.newLatLng(posisiabsen));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
LatLng posisiabsen = place.getLatLng(); ////your lat lng
Log.e("lar",place.getLatLng().latitude+" "+place.getLatLng().longitude );
lat = place.getLatLng().latitude;
lng = place.getLatLng().longitude;
if(googleMap != null) {
googleMap.moveCamera(CameraUpdateFactory.newLatLng(posisiabsen));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
Log.e("pl", "Place: " + place.getName());//get place details here
}
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.e("pl", "An error occurred: " + status);
}
});
googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
LatLng midLatLng = googleMap.getCameraPosition().target;
lat = midLatLng.latitude;
lng = midLatLng.longitude;
Log.e("location",midLatLng.latitude+" "+midLatLng.longitude);
}
});
}
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Fragment fr = new fragmentC();
FragmentManager fm = getFragmentManager();
ragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
public void onDestroyView() {
super.onDestroyView();
if (getActivity() != null) {
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(placeAutoComplete);
fragmentTransaction.commit();
}
}
}}
和片段C
public class fragmentC extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_c,container,false);
}}
主要活动
public class Add_Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
if (Build.VERSION.SDK_INT >= 21) {
try {
getWindow().setStatusBarColor(ContextCompat.getColor(Add_Main.this, R.color.scolor));
}
catch (Exception e)
{
}
}
}
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_place);
if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0){
try {
fragment.getFragmentManager().popBackStack();
}catch (Exception e){
Log.e("error",e.getMessage().toString());
}
}else {
super.onBackPressed();
}
}
}
程序运行良好,但是我有一个问题是当我从片段B移到片段C时 然后我按设备上的“主页”按钮,活动将进入“暂停”状态 将引发以下错误
java.lang.IllegalStateException:保存失败状态:活动的PlaceAutocompleteFragment {98d9b08}已清除索引:-1
我猜是因为从片段B移到C后在ondestory调用时我要删除PlaceAutocompleteFragment
所以我尝试了它来更新这样的代码
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
似乎出错了,但是当我使用onbackprssed从片段C返回到B时出现了问题
将引发另一个错误,我在PlaceAutocompleteFragment的xml中有重复的ID
android.view.InflateException:二进制XML文件第62行:二进制XML文件第62行:夸大类片段的错误 原因:android.view.InflateException:二进制XML文件行#62:错误夸大了类片段 引起原因:java.lang.IllegalArgumentException:二进制XML文件第62行:重复的ID 0x7f1200e8,标记为null,o
知道我该如何处理吗?我需要保持碎片状态,但似乎我没有正确处理PlaceAutocompleteFragment状态,因此会导致错误