Android:Class Fragment - 错误膨胀类映射片段

时间:2016-09-17 01:12:10

标签: java android google-maps android-fragments google-play-services

我是StackOverflow的新手,对不起我可能出现的错误。

我只想在我的应用程序中插入一个简单的Google地图。

我犯了这个错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.luiza.mapagps / com.example.luiza.mapagps.GPS}:   android.view.InflateException:二进制XML文件行#12:二进制XML   文件行#12:错误膨胀类片段

我的xml是:

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.luiza.mapagps.GPS">

<fragment
    android:id="@+id/mapGoogle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>

</RelativeLayout>

我已经尝试过使用:android:name="com.google.android.gms.maps.MapFragment"

这是我的 MainActivity.java:

package com.example.luiza.mapagps;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.LatLng;


public class GPS extends FragmentActivity
    implements OnMapReadyCallback
    {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps);

    MapFragment mapFragment = (MapFragment) getFragmentManager()
            .findFragmentById(R.id.mapGoogle);
    mapFragment.getMapAsync(this);
}
    @Override
    public void onMapReady(GoogleMap map) {

        map.addMarker(new MarkerOptions()
                .position(new LatLng(0, 0))
                .title("Here"));

        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        CameraPosition googlePlex = CameraPosition.builder()
                .target(new LatLng(37.4219999,-122.0862462))
                .zoom(16)
                .bearing(0)
                .tilt(45)
                .build();

        map.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));

    }
}

这是我的 Manifest:

<?xml version="1.0" encoding="utf-8"?>

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".GPS">
        <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.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.geo.Im using my API"
        android:value="I'm using my API"/>
</application>

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

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

这是我的 Build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.2"

defaultConfig {
    applicationId "com.example.luiza.mapagps"
    minSdkVersion 19
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.android.gms:play-services-maps:9.4.0'

我试过了:

  1. 清理并重建项目;

  2. 使用:compile&#39; com.google.android.gms:play-services-maps:9.4.0&#39;

  3. 更改android:name和class

  4. 导入android.support.v4.app.Fragment;

  5. 将此添加到清单文件:

  6. 错误记录

    FATAL EXCEPTION: main
                                                                         Process: com.example.luiza.mapagps, PID: 3432
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luiza.mapagps/com.example.luiza.mapagps.GPS}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
                                                                             at android.app.Activity.setContentView(Activity.java:2166)
                                                                             at com.example.luiza.mapagps.GPS.onCreate(GPS.java:23)
                                                                             at android.app.Activity.performCreate(Activity.java:6237)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                          Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                                             at android.app.Activity.setContentView(Activity.java:2166) 
                                                                             at com.example.luiza.mapagps.GPS.onCreate(GPS.java:23) 
                                                                             at android.app.Activity.performCreate(Activity.java:6237) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                          Caused by: java.lang.RuntimeException: API key not found.  Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
                                                                             at maps.y.k.a(Unknown Source)
                                                                             at maps.v.c.a(Unknown Source)
                                                                             at com.google.android.gms.maps.internal.CreatorImpl.b(Unknown Source)
                                                                             at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(Unknown Source)
                                                                             at vl.onTransact(:com.google.android.gms.DynamiteModulesB:62)
                                                                             at android.os.Binder.transact(Binder.java:387)
                                                                             at qle.newMapFragmentDelegate(:com.google.android.gms:179)
                                                                             at com.google.android.gms.maps.internal.CreatorImpl.newMapFragmentDelegate(:com.google.android.gms:101)
                                                                             at qld.onTransact(:com.google.android.gms:62)
                                                                             at android.os.Binder.transact(Binder.java:387)
                                                                             at com.google.android.gms.maps.internal.zzc$zza$zza.zzag(Unknown Source)
                                                                             at com.google.android.gms.maps.MapFragment$zzb.zzbow(Unknown Source)
                                                                             at com.google.android.gms.maps.MapFragment$zzb.zza(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza.zza(Unknown Source)
                                                                             at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
                                                                             at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source)
                                                                             at android.app.Fragment.onInflate(Fragment.java:1359)
                                                                             at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2200)
                                                                             at android.app.FragmentController.onCreateView(FragmentController.java:98)
                                                                             at android.app.Activity.onCreateView(Activity.java:5546)
                                                                             at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36)
                                                                             at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
                                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
                                                                             at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                                             at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
                                                                             at android.app.Activity.setContentView(Activity.java:2166) 
                                                                             at com.example.luiza.mapagps.GPS.onCreate(GPS.java:23) 
                                                                             at android.app.Activity.performCreate(Activity.java:6237) 
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    

    我真的需要帮助,我知道有很多类似的问题,但没有人回答我的问题。

    提前谢谢。

    我不是来自北美,对于任何英语错误都很抱歉。

1 个答案:

答案 0 :(得分:2)

添加地图片段的权限:

 <permission
        android:name="com.example.luiza.mapagps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
<uses-permission android:name="com.example.luiza.mapagps.permission.MAPS_RECEIVE" />

在您的布局文件中,将fragment更改为SupportMapFragment

<SupportMapFragment
    android:id="@+id/mapGoogle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>
  

java.lang.RuntimeException:找不到API密钥。检查它是否在AndroidManifest.xml的元素中

显示日志。你遗失API_KEY

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