在 Google 个地图中实现 getMapAsync 时遇到问题
Trouble implementing getMapAsync in Google Maps
我正在尝试在 Android 应用程序的 activity 中放置带有标记的地图。它编译和构建很好,但是当我尝试 运行 activity 应用程序崩溃时,我收到消息 "Unfortunately, AppName has stopped"
来自 Android Studio 的日志给我以下消息:
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 得到了同样的结果。
如有任何帮助,我们将不胜感激。
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()
时膨胀了正确的布局。
我正在尝试在 Android 应用程序的 activity 中放置带有标记的地图。它编译和构建很好,但是当我尝试 运行 activity 应用程序崩溃时,我收到消息 "Unfortunately, AppName has stopped"
来自 Android Studio 的日志给我以下消息:
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 得到了同样的结果。
如有任何帮助,我们将不胜感激。
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()
时膨胀了正确的布局。