3

I a have a drawer layout that displays different fragments. When I change fragments everything is okay however if I try to access my mapfragment twice it displays the following errors:

04-26 22:55:28.816: E/AndroidRuntime(27576): FATAL EXCEPTION: main 04-26 22:55:28.816: E/AndroidRuntime(27576): android.view.InflateException: Binary XML file line #6: Error inflating class fragment 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 04-26 22:55:28.816: E/AndroidRuntime(27576): at com.proj.DisMap.LocMap.onCreateView(LocMap.java:121) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.Fragment.performCreateView(Fragment.java:1699) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.BackStackRecord.run(BackStackRecord.java:682) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.os.Handler.handleCallback(Handler.java:730) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.os.Handler.dispatchMessage(Handler.java:92) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.os.Looper.loop(Looper.java:176) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.ActivityThread.main(ActivityThread.java:5419) 04-26 22:55:28.816: E/AndroidRuntime(27576): at java.lang.reflect.Method.invokeNative(Native Method) 04-26 22:55:28.816: E/AndroidRuntime(27576): at java.lang.reflect.Method.invoke(Method.java:525) 04-26 22:55:28.816: E/AndroidRuntime(27576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 04-26 22:55:28.816: E/AndroidRuntime(27576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 04-26 22:55:28.816: E/AndroidRuntime(27576): at dalvik.system.NativeStart.main(Native Method) 04-26 22:55:28.816: E/AndroidRuntime(27576): Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f05000e, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.app.Activity.onCreateView(Activity.java:4971) 04-26 22:55:28.816: E/AndroidRuntime(27576): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)

here my drawer layout:

public class HomeSM extends FragmentActivity {
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    Fragment fragment = null;

    public Boolean flag =true;
.....
private void displayView(int position) {
        // update the main content by replacing fragments

        switch (position) {
        case 0:         
            fragment = new HomeFragment();          
            break;
        case 1:         
            fragment = new detailfrag();

            break;
        case 2:
            fragment = new LocationListView();          
            break;
        case 3:                     

            fragment = new LocMap(); // here is the mapfragment !!!

            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:

            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
            db.resetTables();
            Intent i = new Intent(getApplicationContext(), Login.class);

            startActivity(i);

            // closing this screen
            finish();
            break;

        default:
            break;
        }

        if (fragment != null) {

            FragmentManager fragmentManager = getFragmentManager();
            //fragmentManager.beginTransaction().hide(fragment);

            fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "No activity instantiated");
        }
    }

here is the mapfragment layout:

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

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

</RelativeLayout>

as for the mapfragment here is teh class:

public class LocMap extends Fragment{

    List<HashMap<String, String>> GlobMarkers;
    private GoogleMap googleMap;
    List<HashMap<String, String>> TempmarkersList;  
    Marker marker;
    private LocationManager locationManager;
    private static final long MIN_TIME = 400;
    private static final float MIN_DISTANCE = 1000;
    ProgressDialog pDialog;
    MarkerOptions markerOptions = null;
    Context context;
    //PolygonOptions pOptions;
    Polygon polygon;
    private LatLng crd;
    private float rtn;
    public String TAG;
    LatLng latlng;
    private LocationRequest lr;
    private LocationClient lc;
    public static View inst = null;
    LatLng zoom;

    ArrayList<Circle> Circles;

    BluetoothAdapter bluetoothAdapter;
    TGDevice tgDevice;

    static int cind=0;

    Fragment frg;

    /*@Override
    public void onCreate(Bundle savedInstanceState) {
        setRetainInstance(true);

    }*/

    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            context = getActivity();

            View myFragmentView = inflater.inflate(R.layout.activity_main, container, false);

            try {
                 initilizeMap();
                } catch (Exception e) {
                Log.e("ERROR", "ERROR IN CODE: " + e.toString());
                e.printStackTrace();
            }

            return myFragmentView;

     }
....
@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        }
....

    @Override   
    public void onDestroyView() 
     {
             super.onDestroyView();

             Fragment fragment = (getFragmentManager().findFragmentById(R.id.map)); 
             if (fragment != null){ 
                 getFragmentManager().beginTransaction().remove(fragment).commit();
                 FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
                 ft.remove(fragment);
                 ft.commit();
             }
}

I have tried different solutions online like switching to supportmapfragment but it is giving me the same errors (also my manifest contains all the permissions and dependencies. And I have inmported and added the googlep play library correctly). Also I tried manipulating the ondestroy by checking if current fragment is null before removing but no luck. I have been struggling the whole day with this and I am desperate can anyone please help me with this issue.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Estarossa
  • 585
  • 4
  • 21

0 Answers0