NullPointerException异常当试图将标记在谷歌地图V2(NullPointerExc

2019-09-26 16:56发布

当我尝试把标记在谷歌地图placeMarker方法,我得到的NullPointerException

NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference`
I/DpmTcmClient: RegisterTcmMonitor from: org.apache.http.impl.conn.TcmIdleTimerMonitor
W/DynamiteModule: Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule: Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:2
I/DynamiteModule: Selected remote version of com.google.android.gms.googlecertificates, version >= 2
D/GoogleCertificates: com.google.android.gms.googlecertificates module is loaded
E/url: http://maps.googleapis.com/maps/api/geocode/json?latlng=35.7742618,51.3596331&sensor=true
E/Lat Long to Addresss:
E/Googlemap: null
D/GoogleCertificatesImpl: Fetched 363 Google certificates
I/b: Sending API token request.
E/b: Authentication failed on the server.
E/Google Maps Android API: Authorization failure
Ensure that the "Google Maps Android API v2" is enabled.
                                                                                 Ensure that the following Android Key exists:

此外,我用getAsyncMap和OnMapReady()内OnMapReady我调用Asynktask获得用户位置和onPostExecute placeMarker方法被调用。:

public void placeMarker(LatLongDetails user_latlongobj2,
final Context contextPlace) {


try {
if (googlemap == null) {
    intialiseMap();
    animatecamera(user_latlongobj);
}
if (LoginDetails.Address.length() < 1) {
    LoginDetails.Address = "Getting your location .....";
}
googlemap.clear();
marker = new MarkerOptions().position(
        new LatLng(user_latlongobj2.user_latitude,
                user_latlongobj2.user_longitude)).title(
        LoginDetails.Address);

System.out.println("This is the Address" + LoginDetails.Address);

marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));


googlemap.addMarker(marker).showInfoWindow();
System.out.println("PLACING MARKER" + LoginDetails.Address);
if (marker == null || contextPlace == null) {
    Intent in =new Intent(this,ProfileActivity1.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(in);
}
else
fixmarker(marker, contextPlace);

} catch (Exception e) {
fixmarker(marker, contextPlace);
Log.d("Profileactivity", "" + e);
  } 

}

所有这些问题来了,当我试图改变的GetMap()来getAsyncMap()还请检查该链接上和整个整个活动的GitHub

我删除试图赶上来了解详情约error.here是logcat的:

enter code herejava.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                            at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
                                                                            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
                                                                            at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:551)
                                                                            at android.content.Context.getString(Context.java:413)
                                                                            at com.example.cabbookinghome.ProfileActivity1.intialiseMap(ProfileActivity1.java:280)
                                                                            at com.example.cabbookinghome.ProfileActivity1.placeMarker(ProfileActivity1.java:327)
                                                                            at epbit.helper.ConversionTaskLatLonLoc.onPostExecute(ConversionTaskLatLonLoc.java:130)
                                                                            at epbit.helper.ConversionTaskLatLonLoc.onPostExecute(ConversionTaskLatLonLoc.java:24)
                                                                            at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                            at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)

Answer 1:

3天后搜索和它在这里工作终于worked.the真正的问题是什么的GoogleMap为null,这意味着地图不loaded.I使用try和catch,但你永远不应该只是赶上和异常,什么也不做。 记录异常后,检查我发现这个日志:

com.google.android.gms.maps.MapFragment.getMap()' on a null object reference

首先,我试过此链接谷歌地图返回NullPointerException异常谷歌地图Android版V2

googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

然后getChildFragmentManager成为红色表示,而不是如在本描述链接getChildFragmentManager()不能被解析,或者不能被referenced.using getSupportFragmentManager()( getChildFragmentManager()不能得到解决或不能被引用 )终于解决了这个问题,并且地图已经装载

googlemap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

请注意,如果你的类扩展自AppCompatActivity然后,您需要使用getSupportFragmentManager()因为getChildFragmentManager()是一个片段的方法不能从访问AppCompatActivity



Answer 2:

这是因为谷歌地图目标后,如果声明澄清。 删除googlemap.clear()和尝试。



Answer 3:

您的地图不初始化或不要让你的位置。 检查位置传感器被激活与否。 如果没有,请积极它。 你可以试试这个代码。

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);


    }


文章来源: NullPointerException When Trying to place Marker on Google Map V2