简体   繁体   中英

google maps activity on android

i want to learn how to use google maps through android studio and i try google maps activity on android studio but i got this error i already place my googleMap API on my code

03-04 01:17:43.584 3419-3419/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.example.dennis.peta, PID: 3419
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dennis.peta/com.example.dennis.peta.MapsActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: 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 #2: Binary XML file line #2: 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.dennis.peta.MapsActivity.onCreate(MapsActivity.java:21)
                                                 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 #2: 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.example.dennis.peta.MapsActivity.onCreate(MapsActivity.java:21) 
                                                 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.ao.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:357)
                                                 at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                 at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)

and my code is java

    package com.example.dennis.peta;

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.MapFragment;
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));
    }
}

my 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.example.dennis.peta.MapsActivity" />

please help me i am new to android

I think you done something wrong, so I will add simple MapFragment with code.

First add to your builde.gradle this dependency:

 compile 'com.google.android.gms:play-services-maps:8.4.0'

Next create new class, which is fragment responsible for displaying Map.

public class MapFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        return view;
    }
}

It requires layout for this fragment, so here it is fragment_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView android:id="@+id/maps"
                                         android:layout_width="match_parent"
                                         android:layout_height="match_parent" />

</RelativeLayout>

Here is activity for wrapping fragment - MapActivity.java

public class MapActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        MapFragment mapFragment = new MapFragment();
        getFragmentManager().beginTransaction().add(R.id.fragment_container, mapFragment).commit();
    }
}

and the last one is layout for this activity - activity_map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container">
    </FrameLayout>

</RelativeLayout>

Also remember that you have generate API Key for Google Maps and add it to Manifest, which is described detailed here .

Hope now it's working !

EDITED

Your question:

"...want to learn how to use google maps through android studio"

A lot of what I wrote below can be a helpful checklist for your case, but if you want to learn more about Google Maps in Android Studio, it might be best, as I have done below, to start with a basic Blank Activity, MainActivity, and then add some Google Map elements. In the example I provide here, I start with a Blank Activity and add a simple fragment layout file which allows me to, at a minimum, test that Google Maps Android API works with a new project. I'm not relying on the Android Studio's Google Maps Activity Template provided in the New Project "Add an activity to Mobile." You can certainly learn a few things from it, but it might be better to start from scratch.

  1. Are you testing on a physical device? If not, try it on a physical device. If you haven't already done it, turn USB Debugging on your device on. http://developer.android.com/tools/device.html

  2. Are your location services on your device turned on? Find out by going onto your physical device Settings > Location (under Personal) and see that the slider is On .

  3. Did you create a Google Maps API Key? If not, create one. In the following link, look at Step 4 for instructions: https://developers.google.com/maps/documentation/android-api/start In summary:

    • Go to console.developers.google.com

    • Log in and Create a Project (if you haven't already)

    • Create your Google Maps API Key: go to Google APIs by clicking the big blue button, click on the Google Maps Android API link, click the blue Enable button. If you haven't done this before, you'll get the following message: "This API is enabled, but you can't use it in your project until you create credentials. Click "Go to Credentials" to do this now (strongly recommended)." When you go to the Credentials page, it should automatically choose Google Maps Android API for you in the first drop down menu: Which API are you using? (if it doesn't, you will need to), and then go to the second drop down menu Where will you be calling the API from? and select choose Android . Create an API Key Name - my suggestion is just to name it something that relates to the project, but you can call it whatever you like. Add the package name and fingerprint later. Then click the button Create API Key .

    • Copy and paste the API Key close to the end of your AndroidManifest.xml eg

        </application> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="YOUR API KEY HERE"/> </manifest> 
  4. Add the 4 permissions below to your AndroidManifest.xml between the manifest tag and the beginning of the application tag eg

     <?xml version="1.0" encoding="utf-8"?> <manifest package="YOUR PACKAGE NAME" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="ANDROID.PERMISSION.INTERNET"/> <uses-permission android:name="ANDROID.PERMISSION.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <application android:allowBackup="true"... 
  5. Also make sure you have the necessary libraries and SDK packages:

    • Android Support Repository
    • Google Repository
    • Google Play Services
    • Android Support Library

Go to your build.gradle(app) file and add the Google Play dependencies, the line: compile 'com.google.android.gms:play-services:8.4.0' eg

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services:8.4.0'
}
  1. Then add the following to your AndroidManifest.xml which will verify the Google Play Services Library in your project:

     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> 
  2. And then in MainActivity.java add the maps package to your import statements:

     import com.google.android.gms.maps.GoogleMap; public class MainActivity extends AppCompatActivity { 
  3. Since you're using a fragment, I'll stick with that here:

I create a new layout (right-click on res/layout, New > Layout Resource File) and call it activity_map with root of fragment .

In the activity_map, I've kept most of your fragment code:

<?xml version="1.0" encoding="utf-8"?>
<fragment android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
  1. To test the app, one final change is to add the fragment to your MainActivity in place of the activity_main. You should, of course, launch your fragment however you like when you know that this actually runs:

     setContentView(R.layout.activity_map); 

I had same problem. This definitely help you:

In AndroidManifest.xml, add the following element as a child of the <application> element, by inserting it just before the closing </application> tag :

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

more details: https://developers.google.com/maps/documentation/android-api/signup#add_the_api_key_to_your_application

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