其他分享
首页 > 其他分享> > android-由于膨胀异常,Google Map无法加载

android-由于膨胀异常,Google Map无法加载

作者:互联网

我在应用程序中使用的是Google Maps v2.密钥已正确生成,所有权限都存在于清单中.我遵循了Google Maps v2的教程.我遵循了每一个步骤,但是没有用.请帮忙

日志

03-04 01:25:23.145: E/Trace(19467): error opening trace file: No such file or directory (2)
03-04 01:25:23.293: E/AndroidRuntime(19467): FATAL EXCEPTION: main
03-04 01:25:23.293: E/AndroidRuntime(19467): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vogella.android.locationapi.maps/com.appscourt.jooging.track.map.health.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.os.Looper.loop(Looper.java:137)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.main(ActivityThread.java:5059)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at java.lang.reflect.Method.invokeNative(Native Method)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at java.lang.reflect.Method.invoke(Method.java:511)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at dalvik.system.NativeStart.main(Native Method)
03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.setContentView(Activity.java:1893)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.appscourt.jooging.track.map.health.MainActivity.onCreate(MainActivity.java:20)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.performCreate(Activity.java:5058)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-04 01:25:23.293: E/AndroidRuntime(19467):    ... 11 more
03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4242000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.internal.q.v(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.internal.q.u(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment$b.ex(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment$b.a(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.dynamic.a.a(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.app.Activity.onCreateView(Activity.java:4713)
03-04 01:25:23.293: E/AndroidRuntime(19467):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
03-04 01:25:23.293: E/AndroidRuntime(19467):    ... 22 more

main.xml中

<RelativeLayout 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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.MapFragment"
            android:layout_marginBottom="35dip" />
    </LinearLayout>
</RelativeLayout>

MainActivity.Java

public class MainActivity extends FragmentActivity {
private GoogleMap googleMap;
protected LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initializeMap();
}

@Override
public void onBackPressed() {

    finish();
}

private void initializeMap(){

    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();
            }
        }
    }
}    

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vogella.android.locationapi.maps"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.appscourt.jooging.track.map.health.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCV0yduNQJCXrSWNzk6h__6FYaEg_bfCvE"
           />
    </application>

</manifest>

解决方法:

你的logcat清楚地说

 03-04 01:25:23.293: E/AndroidRuntime(19467): Caused by:
 java.lang.IllegalStateException: The meta-data tag in your app's
 AndroidManifest.xml does not have the right value.  Expected 4242000
 but found 0.  You must have the following declaration within the
 <application> element:     <meta-data
 android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version" /> 03-04
 01:25:23.293: E/AndroidRuntime(19467):

将Google Play服务版本添加到您的应用清单中

Edit your application’s AndroidManifest.xml file, and add the
following declaration within the element. This embeds the version of
Google Play services that the app was compiled with.

您需要添加< meta-data>在< application>下标记到您的AndroidManifest.xml中

....<application>
 <meta-data android:name="com.google.android.gms.version"  
 android:value="@integer/google_play_services_version" />
 </application>

这是因为最新的Google Play服务需要一个版本名称,该名称要使用<元数据.. />来提及.在AndroidManifest.xml中

并更改MainActivity扩展Activity

有关更多信息,请访问:: https://developers.google.com/maps/documentation/android

标签:google-maps-api-2,fatal-error,android,inflate-exception
来源: https://codeday.me/bug/20191010/1883149.html