简体   繁体   中英

Google Maps Error Inflating Class Fragment Android

I have been searching the web and cannot seem to find an answer that works for me. I recently made a new project in Android Simulator and chose the Google Maps Template. I followed the instructions at https://developers.google.com/maps/documentation/android-api/start to make sure I had done everything right the first time. To my disgrace, I get the same error.

Firstly, here is my MapsActivity.java:

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

Secondly, my activity_maps.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.xenicdev.mapstest.MapsActivity" />

And finally, my error:

05-13 19:47:40.542 3052-3052/com.xenicdev.mapstest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xenicdev.mapstest, PID: 3052
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xenicdev.mapstest/com.xenicdev.mapstest.MapsActivity}: android.view.InflateException: Binary XML file line #1: Binary XML file line #1: 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 #1: Binary XML file line #1: 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.xenicdev.mapstest.MapsActivity.onCreate(MapsActivity.java:20)
 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 #1: Error inflating class fragment
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
 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.xenicdev.mapstest.MapsActivity.onCreate(MapsActivity.java:20) 
 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.NullPointerException: Attempt to invoke virtual method 'boolean java.io.File.mkdir()' on a null object reference
 at com.google.maps.api.android.lib6.gmm6.m.ad.a(Unknown Source)
 at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
 at com.google.maps.api.android.lib6.gmm6.c.y.a(Unknown Source)
 at com.google.maps.api.android.lib6.e.bd.a(Unknown Source)
 at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
 at com.google.maps.api.android.lib6.e.z.a(Unknown Source)
 at com.google.maps.api.android.lib6.e.y.a(Unknown Source)
 at com.google.android.gms.maps.internal.u.onTransact(SourceFile:107)
 at android.os.Binder.transact(Binder.java:387)
 at com.google.android.gms.maps.internal.IMapFragmentDelegate$zza$zza.onCreateView(Unknown Source)
 at com.google.android.gms.maps.SupportMapFragment$zza.onCreateView(Unknown Source)
 at com.google.android.gms.dynamic.zza$4.zzb(Unknown Source)
 at com.google.android.gms.dynamic.zza.zza(Unknown Source)
 at com.google.android.gms.dynamic.zza.onCreateView(Unknown Source)
 at com.google.android.gms.maps.SupportMapFragment.onCreateView(Unknown Source)
 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
 at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
 at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
 at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
 at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
 ... 19 mo

I had the same problem, but for me it was because I had put the

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

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />

outside the </application> tag in my manifest. Once I moved it inside it, that error went away.

Okay I had this same type of error sometimes ago too. But What I imlplemented was not to use SupportMapFragment instead I use this piece of code below.

MapFragment mapFragment = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

And I changed my activity_maps.xml. Instead to use this;

android:name="com.google.android.gms.maps.SupportMapFragment"

use this

 class="com.google.android.gms.maps.MapFragment"

Hope it solves the problem. You can contact me for details with your googlemap app if you need more help. Just finished building a simple one.

I check the error message and find the tag for API_KEY should be modified as:

<meta-data android:name="com.google.android.geo.API_KEY" 
android:value="your api key"/>

the XML is wrong . you should use

 class="com.google.android.gms.maps.SupportMapFragment"

not

 android:name="com.google.android.gms.maps.SupportMapFragment"

I have not found a true answer to my problem except that after the Google API had updated, I made a new project that worked. I cannot explain what the cause was for this error but have found that the only workaround for me was to wait for the next Google API update and start a new project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM