无法在Google Maps中实现getMapAsync [英] Trouble implementing getMapAsync in Google Maps

查看:106
本文介绍了无法在Google Maps中实现getMapAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android应用程序的活动中放置带有标记的地图.它可以编译并构建良好,但是当我尝试运行该活动时,应用程序崩溃了,并收到消息不幸的是,AppName已停止"

I am trying to put a map with a marker in an activity in an Android app. It compiles and builds fine, but when I try to run the activity the app crashes and I get the message "Unfortunately, AppName has stopped"

Android Studio的日志向我显示以下消息:

The log from Android Studio gives me the following message:

原因:java.lang.NullPointerException:尝试调用虚拟方法>'void> com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.androi> d.gms.maps.OnMapReadyCallback)'在空对象引用上.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method >'void >com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.androi>d.gms.maps.OnMapReadyCallback)' on a null object reference.

任何想法是什么原因造成的?相同的代码在其他应用程序中也能正常工作.为什么在我调用方法getMapAsynch()时将参数"this"视为空对象引用?我也尝试过DayCenter.this,结果相同.

Any ideas what's causing this? This same code has worked fine in other apps. Why is the parameter 'this' considered a null object reference when I call the method getMapAsynch()? I've also tried DayCenter.this with the same results.

任何帮助将不胜感激.

package cetlot.com.sisuyouth;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import android.support.v4.app.FragmentActivity;
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 DayCenter extends ActionBarActivity implements             OnMapReadyCallback {

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

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    // Add a marker for the SISU Day Center, and move the camera.
    LatLng sisudaycenter = new LatLng(35.454439, -97.598672);
    map.addMarker(new MarkerOptions().position(sisudaycenter).title("SISU Day Center"));
    map.moveCamera(CameraUpdateFactory.newLatLng(sisudaycenter));
}

}

这是布局文件.我尝试将"com.google.android.gms.maps.MapFragment"更改为SupportMapFragment,但仍然无法正常工作.

Here's the layout file. I tried changing "com.google.android.gms.maps.MapFragment" to SupportMapFragment, but it still doesn't work.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation = "vertical"
tools:context="cetlot.com.sisuyouth.DayCenterFragment">

<TextView android:text="@string/day_center_address" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView android:text="@string/day_center_address_line1" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView android:text="@string/day_center_address_line2" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView android:text="@string/day_center_address_line3" android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

推荐答案

您的xml使用的是MapFragment,而不是SupportMapFragment.

Your xml is using a MapFragment, not a SupportMapFragment.

将您的xml更改为使用SupportMapFragment,它应该可以工作:

Change your xml to use a SupportMapFragment and it should work:

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

此外,请确保在对setContentView()的调用中使用正确的布局.

Also, make sure that you're inflating the correct layout in the call to setContentView().

这篇关于无法在Google Maps中实现getMapAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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