谷歌地图 - Android应用程序未加载 - 空对象引用 [英] Google Maps - Android App not load - null object reference

查看:231
本文介绍了谷歌地图 - Android应用程序未加载 - 空对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用Google地图集成开发Android应用。目前我很难找到错误,因为代码来自谷歌网站本身,除了它是一个SupportMapFragment。如果您知道使用SupportMapFragment的实际教程也会很棒。实际,因为我认为 .getMap()是折旧的。如果我应该发布整个代码,请告诉我(我是stackoverflow中的新手)

I am currently trying to develop an Android app with google Maps integration. At the moment I have difficulties to find the error, because the code is from the google site itself, except that it is a SupportMapFragment. If you know an actual tutorial with SupportMapFragment would also be great. Actual, because I think .getMap() is depreciated. If I should post the whole code, let me know (I am new in stackoverflow)

如果尝试使用此代码:

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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 MapsFragment extends SupportMapFragment implements OnMapReadyCallback {

其中一个函数是:

    @Override
     public void onResume() {
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
            .findFragmentById(R.id.map);

    supportMapFragment.getMapAsync(this);
}

错误是: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)'

Fragment xml如下所示:

The Fragment xml looks like this:

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


推荐答案

地图片段代表Lollipop你可能需要这个相反。

Map fragment is acting up on Lollipop you may need this instead.

SupportMapFragment supportMapFragment;
if (Build.VERSION.SDK_INT < 21) {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.map);
} else {
    supportMapFragment = (SupportMapFragment) getActivity()
        .getChildFragmentManager().findFragmentById(R.id.map);
}
supportMapFragment.getMapAsync(this);

这篇关于谷歌地图 - Android应用程序未加载 - 空对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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