当应用程序运行时总是返回NULL异常
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
if (map == null) {
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googlemap) {
// TODO Auto-generated method stub
map=googlemap;
// startMap();
}
});
}
Xml代码在这里我使用Map *
<fragment
android:id="@+id/map"
class="com.shahi.driver.locationfiles.TouchableMapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
答案 0 :(得分:1)
您的类将是:
<a href="http://localhost:9999/video.mp4" download> Dowload Video </a>
和 your_layout.xml 将是:
public class YourActivity extends Activity implements OnMapReadyCallback {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
try {
// Loading map
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(final GoogleMap googleMap) {
final LatLng current_position = new LatLng(your_latitude, your_longitude);
MarkerOptions marker = new MarkerOptions().position(current_position).title("Your Address").snippet("Anything");
googleMap.addMarker(marker);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current_position, 12));
googleMap.getUiSettings().setMapToolbarEnabled(true);
googleMap.setMyLocationEnabled(true);
}
}
答案 1 :(得分:0)
这应该有效:
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location ).title("Marker in location "));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
然后你的xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.polaris.project_x.MapsActivity" />
答案 2 :(得分:0)
我可以看到你在XML代码上使用了自定义Map Fragment。通过搜索它的名字,我偶然发现它在GitHub上的代码。
您的代码的问题在于您尝试从布局中获取MapFragment,但您的自定义地图代码正在扩展SupportMapFragment。
因此,要使代码工作,请将MapFragment更改为SupportMapFragment。
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
此外,如果您的BaseActivity正在扩展AppCompatActivity,那么请使用getSupportFragmentManager()而不是getFragmentManager()。