问题描述:

I am using navigation drawer so my whole app is in fragments. First time if I open window - everything works. But if i choose from navigation to go to settings and then back - everything crashes or i get empty window. Log shows - android.view.InflateException: Binary XML file line #7: Error inflating class fragment. I've tried to change android:name to class, but nothing changes. Tried to change MapFragment to SupportFragment. Same thing. I can't change extends Fragment to extends FragmentActivity because of drawer.

here's my code

public class EmployeeMyMove extends Fragment {

View view;

private GoogleMap googleMap;

MarkerOptions marker, emmarker;

Location location;

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

if (view != null) {

ViewGroup parent = (ViewGroup) view.getParent();

if (parent != null)

parent.removeView(view);

}

try {

view = inflater.inflate(R.layout.employee_my_move, container, false);

Log.d("Log", "Tryed");

} catch (InflateException e) {

Log.d("Log", "crashed "+e);

}

initilizeMap();

return view;

}

private void initilizeMap() {

//if (googleMap == null) {

/*latitudeMine = glob.getLat();

longitudeMine = glob.getLng();*/

googleMap = ((MapFragment) getFragmentManager().findFragmentById(

R.id.MyMoveMap)).getMap();

marker = new MarkerOptions().position(new LatLng(0, 0)).title("You are here");

googleMap.addMarker(marker);

CameraPosition cameraPosition = new CameraPosition.Builder().target(

new LatLng(latitudeMine, longitudeMine)).zoom(15).build();

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

//}

}

}

And view:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<fragment

android:id="@+id/MyMoveMap"

android:name="com.google.android.gms.maps.MapFragment"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1" />

</LinearLayout>

Any suggestions on fixing?

网友答案:
You should override 
public void onDestroy() {
    parentView.removeView(this.mMapView);
    super.onDestroy();
    }
Take a look at here

Android mapview with fragments can't be added twice?

网友答案:

Here is full fix (to all that needs help like this)

1. To main activity that creates drawer:

  1. Make sure your main activity that creates drawer extends FrameActivity
  2. Create variable - public static android.support.v4.app.FragmentManager fragmentManager;
  3. Assign it - fragmentManager = getSupportFragmentManager();

2. Your fragment:

  1. Assign googleMap like - googleMap = ((SupportMapFragment) YourMainActivity.fragmentManager .findFragmentById(R.id.location_map)).getMap();
  2. MOST INPORTANT - override:

@Override

public void onDestroyView() {
    super.onDestroyView();
    if (googleMap != null) {
        YourMainActivity.fragmentManager.beginTransaction()
            .remove(YourMainActivity.fragmentManager.findFragmentById(R.id.location_map)).commit();
        googleMap = null;
    }
}

Thanks to Gaurav for idea. Helped to find answer. Thank you!

相关阅读:
Top