在我的应用程序中,我使用导航抽屉从一个片段移动到另一个片段。
如果我从about_us片段转移到另一个片段,那么如果我再次回到相同的状态,则活动不会恢复,相反应用程序将被关闭。
任何人都可以告诉我代码中的问题,请告诉我修复此问题的解决方案。
public class AboutUsFragment extends Fragment {
public AboutUsFragment(){}
private GoogleMap gmap;
static final double latitude = 33.12615;
static final double longitude = 80.21932;
TextView aboutUs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container, false);
initilizeMap();
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
View aboutUs = getActivity().findViewById(R.id.about_us_content_view);
try {
// Loading map
initilizeMap();
//To enable compass
gmap.getUiSettings().setCompassEnabled(true);
//To locate the school - Creating Marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("School Location");
// adding marker
gmap.addMarker(marker);
//We assigned latitude and longitude in Coordinate
LatLng coordinate = new LatLng(latitude, longitude);
CameraUpdate schoolLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
gmap.animateCamera(schoolLocation);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(coordinate) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
private void initilizeMap()
{
if (gmap == null) {
gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (gmap == null) {
Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
我的about_us.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="220dp" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/map"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/about_us_heading_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/about_us"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/about_us_content_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/abouts_us_content"
android:paddingTop="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
因为我对Fragment完全陌生。有人请告诉我一个noob修复它的方法。非常感谢。
答案 0 :(得分:1)
Fragment with mapFragment in layout crashes when you resume cause for a fragment onCreateView is called every time you visit it every time.
Try to make your rootView static like below,
public static View mRootView;
public static boolean mOnRangeChange= false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(mRootView==null){
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
}else{
ViewGroup parent = (ViewGroup) mRootView.getParent();
if (parent != null)
parent.removeView(mRootView);
}
map_layout = (FrameLayout) mRootView.findViewById(R.id.map_layout);
mcategory = (Button) mRootView.findViewById(R.id.contact_categories);
mSharedpreferenceUtility = SharedpreferenceUtility.getInstance(getActivity());
mcategory.setOnClickListener(this);
try {
initializeMap(mRootView);
} catch (Exception e) {
e.printStackTrace();
}
return mRootView;
}
答案 1 :(得分:0)
在提交fragmnetTransaction
之前将片段添加到backstackFragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.content_frame, fragment).commit();