我在动作栏活动中使用了导航抽屉现在我在相同的活动中使用了多个片段,但问题是当我第一次加载mapfragment时它显示完美但是当我按下后退按钮我的应用程序崩溃时显示非法参数异常。
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
答案 0 :(得分:14)
使用此代码
@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();
}
}
如果这有帮助,请将此标记为
答案 1 :(得分:2)
不是在布局中声明de SupportMapFragment,而是以编程方式执行,并确保使用getChildFragmentMananger而不是经典的getFragmentManager()来创建片段。
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map_root, mMapFragment);
fragmentTransaction.commit();
保留此SupportMapFragment mMapFragment,因为您需要它来检索GoogleMap对象:
GoogleMap map = mMapFragment.getMap();
答案 2 :(得分:0)
也像这样工作
@Override
public void onDestroyView() {
try{
SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapviews));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}catch(Exception e){
}
super.onDestroyView();
}