Google在Android上的地图活动 [英] google maps activity on android

查看:58
本文介绍了Google在Android上的地图活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何通过android studio使用google maps,我尝试在android studio上使用google maps活动,但是遇到了这个错误,我已经将googleMap API放在了我的代码上

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)

我的代码是 Java

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

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

请帮助我,我是android的新手

please help me i am new to android

推荐答案

已编辑

您的问题:

"......想学习如何通过android studio使用Google地图"

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

我在下面写的很多内容都可以作为您的案例的有用清单,但是,如果您想了解更多有关Android Studio中的Google Maps的信息,最好像下面所做的那样,从基本的Blank开始活动,MainActivity,然后添加一些Google Map元素.在此处提供的示例中,我从一个空白活动开始,然后添加一个简单的片段布局文件,该文件至少允许我测试Google Maps Android API是否可以与新项目一起使用.我不依赖新项目向移动设备添加活动"中提供的Android Studio的Google Maps活动模板.您当然可以从中学到一些东西,但是从头开始可能会更好.

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. 您是否正在物理设备上进行测试?如果不是,请在物理设备上尝试.如果尚未完成,请打开设备上的USB调试. http://developer.android.com/tools/device.html

您的设备上的位置服务是否已打开?通过转到物理设备的设置">位置"(在个人"下)进行查找,然后查看滑块是否处于打开状态.

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.

您是否创建了Google Maps API密钥?如果没有,请创建一个. 在以下链接中,查看第4步以获取说明: https://developers.google.com/maps/documentation/android-api/start 总结:

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:

  • 转到console.developers.google.com

  • Go to console.developers.google.com

登录并创建一个项目(如果尚未创建)

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

创建您的Google Maps API密钥:单击蓝色的大按钮转到Google API,单击Google Maps Android API链接,然后单击蓝色的启用按钮.如果您以前没有做过此操作,则会收到以下消息:已启用此API,但只有在创建凭据后才能在项目中使用它. 点击转到凭据"以立即执行此操作(强烈建议)."当您转到凭据"页面时,它将在第一个下拉菜单中自动为您选择 Google Maps Android API :您正在使用哪个API?(如果没有,则需要使用),然后转到第二个下拉菜单您将从何处调用该API?选择select Android .创建一个API密钥名称-我的建议是为它命名一个与项目相关的名称,但是您可以随意调用它.稍后添加程序包名称和指纹,然后单击按钮创建API密钥.

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.

将API密钥复制并粘贴到AndroidManifest.xml末尾附近,例如

Copy and paste the API Key close to the end of your AndroidManifest.xml e.g.

  </application>

  <meta-data android:name="com.google.android.maps.v2.API_KEY"
   android:value="YOUR API KEY HERE"/>

</manifest>

将以下4个权限添加到清单清单标记和应用程序标记开头之间的AndroidManifest.xml中,例如

Add the 4 permissions below to your AndroidManifest.xml between the manifest tag and the beginning of the application tag e.g.

<?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"...

  • 还请确保您具有必需的库和SDK包:

  • Also make sure you have the necessary libraries and SDK packages:

    • Android支持存储库
    • Google存储库
    • Google Play服务
    • Android支持库

    转到您的build.gradle(app)文件并添加Google Play依赖项,即以下行:编译"com.google.android.gms:play-services:8.4.0",例如

    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' e.g.

    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. 然后将以下内容添加到您的AndroidManifest.xml中,这将验证您项目中的Google Play服务库:

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

  • 然后在MainActivity.java中将maps包添加到您的导入语句中:

  • 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 {
    

  • 由于您使用的是片段,因此我将在此处坚持使用:

  • Since you're using a fragment, I'll stick with that here:

    我创建一个新布局(右键单击res/layout,新建>布局资源文件),并将其命名为 activity_map ,其根目录为 fragment .

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

    在activity_map中,我保留了您的大部分片段代码:

    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. 要测试该应用程序,最后一项更改是将片段添加到您的MainActivity中,以代替activity_main.当然,您应该启动片段,但是当您知道它确实在运行时,您会喜欢它:

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

  • 这篇关于Google在Android上的地图活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆