我在Android中使用GoogleMaps已经有一段时间没有任何问题但现在突然没有做任何更改我在尝试从XML中膨胀MapView时遇到了InflateException。我已经找到了解决方案,而且我发现的所有内容都是正确地添加了库,我知道我已经完成了以前的工作。这是我的代码:
使用MapView的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="@color/white"
/>
OnCreateView()方法,我在那里给MapView充气:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.home_fragment, container, false);
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
}
错误日志:
08-23 09:42:14.340: E/AndroidRuntime(30030): FATAL EXCEPTION: main
08-23 09:42:14.340: E/AndroidRuntime(30030): android.view.InflateException: Binary XML file line #2: Error inflating class com.google.android.gms.maps.MapView
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-23 09:42:14.340: E/AndroidRuntime(30030): at net.multieducator.telaviv.MainActivity$Map.onCreateView(MainActivity.java:948)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.View.measure(View.java:15635)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.View.measure(View.java:15635)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.View.measure(View.java:15635)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.View.measure(View.java:15635)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-23 09:42:14.340: E/AndroidRuntime(30030): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2213)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.View.measure(View.java:15635)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.Choreographer.doCallbacks(Choreographer.java:579)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.Choreographer.doFrame(Choreographer.java:548)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.os.Handler.handleCallback(Handler.java:800)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.os.Handler.dispatchMessage(Handler.java:100)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.os.Looper.loop(Looper.java:194)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.app.ActivityThread.main(ActivityThread.java:5431)
08-23 09:42:14.340: E/AndroidRuntime(30030): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 09:42:14.340: E/AndroidRuntime(30030): at java.lang.reflect.Method.invoke(Method.java:525)
08-23 09:42:14.340: E/AndroidRuntime(30030): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-23 09:42:14.340: E/AndroidRuntime(30030): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-23 09:42:14.340: E/AndroidRuntime(30030): at dalvik.system.NativeStart.main(Native Method)
08-23 09:42:14.340: E/AndroidRuntime(30030): Caused by: java.lang.reflect.InvocationTargetException
08-23 09:42:14.340: E/AndroidRuntime(30030): at java.lang.reflect.Constructor.constructNative(Native Method)
08-23 09:42:14.340: E/AndroidRuntime(30030): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-23 09:42:14.340: E/AndroidRuntime(30030): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-23 09:42:14.340: E/AndroidRuntime(30030): ... 46 more
08-23 09:42:14.340: E/AndroidRuntime(30030): Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
08-23 09:42:14.340: E/AndroidRuntime(30030): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
08-23 09:42:14.340: E/AndroidRuntime(30030): at com.google.android.gms.maps.MapView.<init>(Unknown Source)
08-23 09:42:14.340: E/AndroidRuntime(30030): ... 49 more
感谢您的帮助:)
答案 0 :(得分:1)
试试这样:
<强> layout.xml 强>
<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>
<强> MainActivity.java 强>
public class MainActivity extends Activity {
// Google Map
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
try {
// Loading map
initilizeMap();
// Changing map type
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
// googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
// googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
// Showing / hiding your current location
googleMap.setMyLocationEnabled(true);
// Enable / Disable zooming controls
googleMap.getUiSettings().setZoomControlsEnabled(false);
// Enable / Disable my location button
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
// Enable / Disable Compass icon
googleMap.getUiSettings().setCompassEnabled(true);
// Enable / Disable Rotate gesture
googleMap.getUiSettings().setRotateGesturesEnabled(true);
// Enable / Disable zooming functionality
googleMap.getUiSettings().setZoomGesturesEnabled(true);
double latitude = 21.3235752;
double longitude =74.1881265;
// lets place some 10 random markers
// random latitude and logitude
double[] randomLocation = createRandLocation(latitude,
longitude);
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(randomLocation[0], randomLocation[1]))
.title(" Mapview Test ");
//marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
googleMap.addMarker(marker);
// Move the camera to last position with a zoom level
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(randomLocation[0],
randomLocation[1])).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
initilizeMap();
}
/**
* function to load map If map is not created it will create it for you
* */
@SuppressLint("NewApi")
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
/*
* creating random postion around a location for testing purpose only
*/
private double[] createRandLocation(double latitude, double longitude) {
return new double[] { latitude + ((Math.random() - 0.5) / 500),
longitude + ((Math.random() - 0.5) / 500),
150 + ((Math.random() - 0.5) * 10) };
}
}
清单权限和API
<permission
android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- meta-data before </application> tag -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDxEkfvGvKy.........your api key" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
希望这可以帮到你