【问题标题】:Trouble implementing getMapAsync in Google Maps在 Google 地图中实现 getMapAsync 时遇到问题
【发布时间】:2015-08-01 01:03:46
【问题描述】:

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

Android Studio 的日志给了我以下信息:

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

任何想法是什么导致了这个?相同的代码在其他应用程序中运行良好。为什么当我调用方法 getMapAsynch() 时参数“this”被视为空对象引用?我也尝试过 DayCenter.this,结果相同。

任何帮助将不胜感激。

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,但它仍然不起作用。

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

【问题讨论】:

  • 这不是this 那是空的,它是mapFragment。发布您的 xml 布局。
  • 谢谢,丹尼尔。布局已发布。有什么想法吗?
  • 谢谢,丹尼尔。布局已发布。有什么想法吗?

标签: android maps


【解决方案1】:

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

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

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

另外,请确保您在对 setContentView() 的调用中填充了正确的布局。

【讨论】:

  • 丹尼尔,感谢您的回复。你是对的,它需要是 SupportMapFragment。但除此之外还有一些其他问题。我将布局更改为 SupportMapFragment,但它仍然崩溃并显示相同的错误消息。有什么想法吗?
  • @user2017020 我刚刚注意到您的布局似乎是针对片段的。您是否在片段中膨胀该布局?如果该布局不是您在 Activity 中调用 setContentView() 时使用的布局,那就可以解释了。
  • 你是对的。这就是问题所在。再次感谢。出于某种原因,当您创建新活动时,Studio 会同时创建布局和片段布局。
  • @junglejim 不错!它现在工作了吗?至于 Fragment 的东西,只要确保在创建 Activity 时修改设置即可。
  • 它工作正常。谢谢你的帮助。现在我可以开始这个地图活动了。
猜你喜欢
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多