ho utilizzato il cassetto di navigazione in attività barra delle azioni ora ho usato più frammenti nella stessa attività ma il problema è quando carico per la prima volta mapfragment è visualizzato perfettamente ma quando premo backbutton la mia app si blocca è visualizza l'eccezione di argomento illegale.L'app si arresta in modo anomalo quando si usa map in fragment in android?
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
utilizzando frammento all'interno di un frammento. – Raghunandan
È necessario rimuovere Fragment quando si preme indietro .. in 'onDestroyView (....)' –
un problema viene quando apro la seconda volta si bloccherà di nuovo e visualizzerà lo stesso errore – Mahesh