gpt4 book ai didi

android - 尝试在 Google Map V2 上放置标记时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 19:28:56 26 4
gpt4 key购买 nike

当我尝试使用 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() 时,所有这些问题都出现了另外请检查这个 link和完整的整个 Activity GitHub

我删除了 try 和 catch 以查找有关错误的更多详细信息。这是 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)

最佳答案

经过 3 天的搜索和处理,这里终于奏效了。真正的问题是 googlemap 为 null,这意味着 map 未加载。我使用了 try 和 catch,但你永远不应该只是捕捉和异常而什么都不做。在记录异常之后,检查我发现的日志:

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

首先我尝试了这个链接 Google Map returning nullpointerexception Google Maps Android V2

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

然后 getChildFragmentManager 变为红色,表示 getChildFragmentManager () 无法解析或无法引用。使用 getSupportFragmentManager() 代替,如此链接中所述( getChildFragmentManager () cannot be resolved or cannot be referenced ) 终于解决了问题, map 已经加载

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

请注意,如果您的类从 AppCompatActivity 扩展,那么您需要使用 getSupportFragmentManager() 因为 getChildFragmentManager() 是 Fragment 的一种方法,无法从 AppCompatActivity

访问

关于android - 尝试在 Google Map V2 上放置标记时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com