我试图在谷歌地图上显示我的GPS位置,并在数据可用时显示其他人的位置。
这是我加载地图片段的片段:
package xyz.fragments;
import android.app.Activity;
import android.app.FragmentManager;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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;
import xyzR;
public class IncidentMapFragment extends SupportMapFragment implements OnMapReadyCallback {
public static IncidentMapFragment newInstance()
{
return new IncidentMapFragment();
}
private SupportMapFragment mapFragment;
private static GoogleMap map;
public static final int UPDATE_MY_CURRENT_LOCATION = 0;
public static final int UPDATE_MY_TEAMS_CURRENT_LOCATIONS = 1;
public static final int UPDATE_ALL_TEAMS_CURRENT_LOCATIONS = 2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.incident_map_fragment, container, false);
//I ALSO tried getFragmentManager()
mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.incident_map);
//THE NULLPOINTER when accessing mapFragment IS AT THIS LINE
mapFragment.getMapAsync(this);
return rootView;
}
public static final Handler updateIncidentMap = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
final int what = msg.what;
switch(what) {
case UPDATE_MY_CURRENT_LOCATION:
Location myCurrentLocation = (Location) msg.obj;
if (map!=null && myCurrentLocation!=null) {
myKeyOntheMap(map,myCurrentLocation);
}
break;
case UPDATE_MY_TEAMS_CURRENT_LOCATIONS:
break;
case UPDATE_ALL_TEAMS_CURRENT_LOCATIONS:
break;
default: break;
}
}
};
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
private static void myKeyOntheMap( GoogleMap map,Location location)
{
LatLng myPos = new LatLng(location.getLatitude(), location.getLongitude());
map.addMarker(new MarkerOptions().position(myPos)
.title("You"));
map.moveCamera(CameraUpdateFactory.newLatLng(myPos));
}
}
incident_map_fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/incident_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
Im getting: Android GoogleMap java.lang.NullPointerException: Try to invoke interface method 'void com.google.maps.api.android.lib6.impl.bo.v((' at code line mapFragment.getMapAsync(this);
有什么建议吗?
谢谢
在片段中,SupportMapFragment
不起作用...解决这个问题 使用MapView
.. 为了MapView
使用fragment
看到这个答案... https://stackoverflow.com/a/19354359/9868485