当前位置: 首页 > 知识库问答 >
问题:

基于XML的MapView的Android InflateException

郎翔
2023-03-14
<?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"
/>
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

谢谢你的帮助:)

共有1个答案

纪翰
2023-03-14

试着这样做:

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) };
    }
}
<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" />
 类似资料:
  • 问题内容: 是否可以在应用程序中同时具有MyBatis的基于XML +注释的配置。 我之所以这样问,是因为在我的应用程序中,我使用的是基于注释的方法。但是在一种情况下,我需要使用IN子句,可以使用 基于XML的配置。 但是,当我启动应用程序时,它似乎无法识别基于注释的映射器,并给了我一个例外。 因此,我想知道是否可以在应用程序中同时具有MyBatis的基于XML + Annotation的配置。请

  • 主要内容:语法,运行项目是一种通知类型,可以确保方法执行前后的通知运行。 以下是通知的语法: 语法 在上面配置中, - 切入点的id。 - 在调用函数之前调用的函数的方法名称。 要了解上述与周围通知(Around Advice)相关的概念,写一个在实现周围通知(Around Advice)的示例。为了简单,可直接复制之前上一篇文章中的例子,打开并使用Eclipse IDE,并按照以下步骤创建一个Spring应用程序: 更

  • 主要内容:语法,运行项目是一种通知类型,可以确保方法执行前后的通知运行。 以下是通知的语法: 语法 在上面配置中, - 切入点的id。 - 在调用函数之前调用的函数的方法名称。 要了解上述与周围通知(Around Advice)相关的概念,写一个在实现周围通知(Around Advice)的示例。为了简单,可直接复制之前上一篇文章中的例子,打开并使用Eclipse IDE,并按照以下步骤创建一个Spring应用程序: 更

  • 主要内容:语法,运行项目之后()是一种通知类型,可以确保在方法执行前运行通知。以下是之后()的通知语法: 语法 在上面配置中, - 切入点的id。 - 在调用函数之前调用的函数的方法名称。 要了解上述与之后通知(After Advice)相关的概念,写一个在实现之后通知(After Advice)的示例。为了简单,可直接复制之前上一篇文章中的例子,打开并使用Eclipse IDE,并按照以下步骤创建一个Spring应用程

  • 主要内容:语法,运行项目之前()是一种通知类型,可以确保在方法执行前运行通知。以下是之前()的通知语法: 语法 在上面配置中, - 切入点的id。 - 在调用函数之前调用的函数的方法名称。 要了解上述与之前通知(Before Advice)相关的概念,写一个在实现之前通知(Before Advice)的示例。为了简单,可直接复制之前上一篇文章中的例子,打开并使用Eclipse IDE,并按照以下步骤创建一个Spring应

  • 问题内容: 我尝试不使用任何xml。 像这样一个:转换为@Bean 问题在这里。 尝试将“ com.cloudlb.domain.User”转换为Class []无效。 错误:投放问题。 先感谢您。 问题答案: