Android 在Google地图中实现getMapAsync时遇到问题

Android 在Google地图中实现getMapAsync时遇到问题,android,maps,Android,Maps,我正在尝试在Android应用程序的活动中放置带有标记的地图。它可以很好地编译和构建,但当我尝试运行该活动时,应用程序崩溃,我收到消息“不幸的是,AppName已停止” Android Studio的日志给了我以下信息: 原因:java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法>'void>com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.androi>d.gms.

我正在尝试在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"/>


您的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()

时使用了正确的布局,它不是
为空,而是mapFragment。发布您的xml布局。谢谢,Daniel。布局已发布。有什么想法吗?谢谢,丹尼尔。布局已发布。有什么想法吗?丹尼尔,谢谢你的回复。你说得对,它需要是SupportMapFragment。但除此之外还有其他一些问题。我将布局更改为SupportMapFragment,它仍然会崩溃,并显示相同的错误消息。有什么想法吗?@user2017020我刚注意到你的布局似乎只是一个片段。你是在一个片段中膨胀布局吗?如果该布局不是您在活动中调用setContentView()时使用的布局,那么这就可以解释了。您是对的。这就是问题所在。再次感谢。由于某些原因,当您创建新活动时,Studio会同时创建布局和片段布局。@junglejim nice!现在可以用了吗?至于片段,只要确保在创建活动时修改设置就行了。它正在工作。谢谢你的帮助。现在我可以开始这个地图活动了。