Mahesh Mahesh - 3 months ago 8
Android Question

App is crashing on back pressed when use map inside fragment in android?

i have used navigation drawer in actionbar activity now i used multiple fragment in same activity but problem is when i first time load mapfragment it is display perfectly but when i press backbutton my app is crashing it is display illegal argument exception.


Mapfragment.java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import com.compass.DirectionsJSONParser;
import com.compass.GPSTracker;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MapFragment extends Fragment {

GoogleMap googlemap;
GPSTracker gps;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map,container,false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

gps=new GPSTracker(getActivity());
googlemap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapviews)).getMap();
MapsInitializer.initialize(getActivity().getApplicationContext());

// current location
double currentlat=gps.getLatitude();
double currentlon=gps.getLongitude();

// destination location
//double destlat=23.048854;
//double destlon=72.516059;

double destlat=22.9833;
double destlon=72.5000;

final LatLng currentpoint = new LatLng(currentlat,currentlon);
final LatLng destinationpoint = new LatLng(destlat,destlon);

// fro add marker on map
googlemap.addMarker(new MarkerOptions()
.position(new LatLng(currentlat, currentlon))
.title("Current Location")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_source)));

googlemap.animateCamera(CameraUpdateFactory.zoomTo(237.0f));
googlemap.moveCamera(CameraUpdateFactory.newLatLng(currentpoint));

googlemap.addMarker(new MarkerOptions()
.position(new LatLng(destlat, destlon))
.title("WebOffice")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_destination)));

/*
String url = getDirectionsUrl(currentpoint, destinationpoint);
DownloadTask downloadTask = new DownloadTask();

// Start downloading json data from Google Directions API
downloadTask.execute(url);
*/

}



fragment_map.xml


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

<fragment
android:id="@+id/mapviews"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>



Logcat


12-09 11:48:05.165: E/AndroidRuntime(9701): FATAL EXCEPTION: main
12-09 11:48:05.165: E/AndroidRuntime(9701): Process: com.weboffice, PID: 9701
12-09 11:48:05.165: E/AndroidRuntime(9701): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-09 11:48:05.165: E/AndroidRuntime(9701): at com.weboffice.MapFragment.onCreateView(MapFragment.java:45)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.os.Handler.handleCallback(Handler.java:733)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.os.Handler.dispatchMessage(Handler.java:95)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.os.Looper.loop(Looper.java:136)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-09 11:48:05.165: E/AndroidRuntime(9701): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 11:48:05.165: E/AndroidRuntime(9701): at java.lang.reflect.Method.invoke(Method.java:515)
12-09 11:48:05.165: E/AndroidRuntime(9701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-09 11:48:05.165: E/AndroidRuntime(9701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-09 11:48:05.165: E/AndroidRuntime(9701): at dalvik.system.NativeStart.main(Native Method)
12-09 11:48:05.165: E/AndroidRuntime(9701): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0b005c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
12-09 11:48:05.165: E/AndroidRuntime(9701): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
12-09 11:48:05.165: E/AndroidRuntime(9701): ... 19 more

Answer

use this code

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

    try {
        Fragment fragment = (getFragmentManager()
                .findFragmentById(R.id.map));
        FragmentTransaction ft = getActivity().getSupportFragmentManager()
                .beginTransaction();
        ft.remove(fragment);
        ft.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and mark this ans up if this helps

Comments