2

我是新的StackOverflow,然後抱歉我的可能的錯誤。Android:類的片段 - 錯誤膨脹類地圖片段

我只想在我的應用程序中插入一個簡單的Google地圖。

我這個錯誤:

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

我的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)); 

    } 
} 

這裏是我的清單:

<?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. 用途:編譯 'com.google.android.gms:發揮服務,地圖:9.4.0'

  3. 變化的android:名稱和類

  4. 進口android.support.v4。 app.Fragment;

  5. 內容添加到manifest文件:

錯誤日誌

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)  

我真的需要幫助,我知道有很多類似的問題,但沒有人回答我的問題。

預先感謝您。

而且我不是來自北美的,那麼對於任何英語錯誤,都很抱歉。

+0

更仔細地閱讀logcat。您會在實際錯誤中看到找不到api密鑰。看看最後由原因造成的,而不是第一個 –

+0

thx。我正在糾正這個問題,我希望沒有其他錯誤 –

+0

如果是這樣,你可能已經保存自己到StackOverflow,通過搜索錯誤文本全部是 –

回答

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

在您的佈局文件,如您的日誌改變fragmentSupportMapFragment

<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 key not found. Check that is in the element of 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" /> 
+0

謝謝,我想我知道我對API key做了什麼,我用錯了,我會做一個新的項目,如果它工作,我會在這裏發佈。 –