java 谷歌地图 - Android 应用程序未加载 - 空对象引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29134914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps - Android App not load - null object reference
提问by Charlie
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)
我目前正在尝试开发一个与谷歌地图集成的 Android 应用程序。目前我很难找到错误,因为代码来自谷歌网站本身,除了它是一个 SupportMapFragment。如果您知道 SupportMapFragment 的实际教程也会很棒。实际,因为我认为.getMap()
是折旧的。如果我应该发布整个代码,请告诉我(我是 stackoverflow 的新手)
If try to use this code:
如果尝试使用此代码:
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 {
where a function is:
其中一个函数是:
@Override
public void onResume() {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
.findFragmentById(R.id.map);
supportMapFragment.getMapAsync(this);
}
The error is: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
错误是: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
The Fragment xml looks like this:
Fragment xml 如下所示:
<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>
回答by puj
It seems like you need to use the Fragment
's SupportFragmentManager
See the answer to this findFragmentById for SupportMapFragment returns null in Android Studio
似乎您需要使用Fragment
's SupportFragmentManager
See the answer to this findFragmentById for SupportMapFragment return null in Android Studio
回答by Xjasz
Map fragment is acting up on Lollipop you may need this instead.
Map fragment 对 Lollipop 起作用,您可能需要它。
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);