我正在使用Google地图开发Android应用。 我的地图片段工作得很好,但在我为我的应用设置了一个设计后,我的地图碎片中出现了这个错误:
08-09 19:59:37.982: E/AndroidRuntime(14374): FATAL EXCEPTION: main 08-09
19:59:37.982: E/AndroidRuntime(14374): Process: pixels.myapp, PID: 14374 08-
09 19:59:37.982: E/AndroidRuntime(14374): android.view.InflateException:
Binary XML file line #2: Error inflating class fragment 08-09 19:59:37.982:
E/AndroidRuntime(14374): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.view.LayoutInflater.inflate(LayoutInflater.java:470) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.view.LayoutInflater.inflate(LayoutInflater.java:398) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
pixels.myapp.FragmentMaps.onCreateView(FragmentMaps.java:70) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.Fragment.performCreateView(Fragment.java:1700) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.Activity.onCreateView(Activity.java:4870) 08-09 19:59:37.982:
E/AndroidRuntime(14374): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.view.LayoutInflater.inflate(LayoutInflater.java:470) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.view.LayoutInflater.inflate(LayoutInflater.java:398) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
pixels.myapp.FragmentMaps.onCreateView(FragmentMaps.java:70) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.Fragment.performCreateView(Fragment.java:1700) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.BackStackRecord.run(BackStackRecord.java:684) 08-09
19:59:37.982:
E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
08-09 19:59:37.982: E/AndroidRuntime(14374): at
android.app.FragmentManagerImpl$1.run(FragmentManager.java:443) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.os.Handler.handleCallback(Handler.java:733) 08-09 19:59:37.982:
E/AndroidRuntime(14374): at
android.os.Handler.dispatchMessage(Handler.java:95) 08-09 19:59:37.982:
E/AndroidRuntime(14374): at android.os.Looper.loop(Looper.java:136) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
android.app.ActivityThread.main(ActivityThread.java:5426) 08-09
19:59:37.982:
E/AndroidRuntime(14374): at java.lang.reflect.Method.invokeNative(Native
Method) 08-09 19:59:37.982: E/AndroidRuntime(14374): at
java.lang.reflect.Method.invoke(Method.java:515) 08-09 19:59:37.982:
E/AndroidRuntime(14374): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1268
) 08-09 19:59:37.982: E/AndroidRuntime(14374): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 08-09
19:59:37.982: E/AndroidRuntime(14374): at
dalvik.system.NativeStart.main(Native Method) 08-09 19:59:37.982:
E/AndroidRuntime(14374): Caused by: java.lang.IllegalArgumentException:
Binary XML file line #2: Duplicate id 0x7f0c0093, tag null, or parent id
0x7f0c0026 with another fragment for com.google.android.gms.maps.MapFragment
08-09 19:59:37.982: E/AndroidRuntime(14374): at
android.app.Activity.onCreateView(Activity.java:4856) 08-09 19:59:37.982:
E/AndroidRuntime(14374): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
和MapFragment.xml:
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
和MapFragment.java:
public class FragmentMaps extends Fragment implements OnMarkerClickListener {
private GoogleMap googleMap;
MapView mMapView;
ArrayList<LatLng> markerPoints;
private Polyline line;
public FragmentMaps() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps,container, false);
markerPoints = new ArrayList<LatLng>();
markerPoints.add(new LatLng(35.8400065, 10.5990825));
markerPoints.add(new LatLng(35.8400030, 10.5990825));
mMapView= (MapView) rootView.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
googleMap.setMyLocationEnabled(true);
googleMap.setOnMarkerClickListener(this);
/**googleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// Already two locations
if(markerPoints.size()>1){
markerPoints.clear();
googleMap.clear();
}
// Adding new item to the ArrayList
markerPoints.add(point);
// Creating MarkerOptions
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(point);
/**
* For the start location, the color of marker is GREEN and
* for the end location, the color of marker is RED.
if(markerPoints.size()==1){
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
}else if(markerPoints.size()==2){
options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
// Add new marker to the Google Map Android API V2
googleMap.addMarker(options);
// Checks, whether start and end locations are captured
if(markerPoints.size() >= 2){
LatLng origin = markerPoints.get(0);
LatLng dest = markerPoints.get(1);
// Getting URL to the Google Directions API
String url = getDirectionsUrl(origin, dest);
DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
}
}
}
);
**/
double latitude = 35.8400065;
double longitude = 10.5990825;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("JetSet Café");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
marker = new MarkerOptions().position(
new LatLng(35.835829, 10.5957462)).title("ToscanaVilla Café");
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude,longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
return rootView;
}
正如我之前所说的,我为我的应用程序设置了一切都很好,但在那之后,我无法获得mu地图片段的工作。
答案 0 :(得分:0)
请使用以下布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
</LinearLayout>
答案 1 :(得分:0)
我只是添加了这个布局,它运行良好:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />