我有一个活动,我只想显示地图。因此,我使用MapView和GoogleMap对象来执行此操作。但是,当我尝试更改地图的地图类型时,我的地图活动中出现了空对象错误。我已经阅读了一些关于这个问题的主题,但我的问题仍然存在。
public class Maps extends AppCompatActivity {
private GoogleMap map;
private MapView map_view
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
Log.i("DEBUG", "Dans Maps");
// MapView to display the further display clicked place
map_view = (MapView)findViewById(R.id.mapview);
map_view.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map_view.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
});
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
map_view.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
map_view.onResume();
}
@Override
protected void onPause() {
super.onPause();
map_view.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
map_view.onLowMemory();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
map_view.onSaveInstanceState(outState);
}
@Override
protected void onStart() {
super.onStart();
//mGoogleApiClient.connect();
}
@Override
protected void onStop() {
//mGoogleApiClient.disconnect();
super.onStop();
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="nicolas.florian.appliflo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.access_network_state" />
<uses-permission android:name="com.tp.lib.tp.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.access_fine_location" />
<uses-permission android:name="android.permission.write_external_storage" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".Welcome"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ConnectUser"
android:label="connectUser">
</activity>
<activity
android:name=".UsersListActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".AddNewUser"
android:label="@string/title_activity_add_new_user" >
</activity>
<activity
android:name=".AddPresent"
android:label="AddPresent" >
</activity>
<activity
android:name=".WebBrowserToRetrieveURL"
android:label="WebBrowserToRetrieveURL" >
</activity>
<activity
android:name=".AfterConnection"
android:label="MainActivity" >
</activity>
<activity
android:name=".PresentList"
android:label="PresentList" >
</activity>
<activity
android:name=".Maps"
android:label="Maps" >
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_KEY"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
/>
</application>
</manifest>
与我的地图活动相关联的布局文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/myLocation"
android:enabled="true"
android:clickable="true"
/>
<Button
android:id="@+id/button_select_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mapview"/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:2)
map
永远不会在onMapReady
之前初始化,因此onCreate
中的所有其他用法都会导致NullPointerException。
你可以试试这个
map_view.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
}
});