【发布时间】: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 布局。 -
谢谢,丹尼尔。布局已发布。有什么想法吗?
-
谢谢,丹尼尔。布局已发布。有什么想法吗?