gpt4 book ai didi

java - AppCompatActivity 中的 GoogleMap 对象为 null

转载 作者:行者123 更新时间:2023-12-02 03:22:15 25 4
gpt4 key购买 nike

我有一个 Activity ,我只想显示 map 。因此,我使用 MapView 和 GoogleMap 对象来执行此操作。但是,当我尝试更改 map 的 map 类型时,我的 map Activity 中出现空对象错误。我已经阅读了有关此类问题的一些主题,但我的问题仍然存在。

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();

}


}

这是我的 list :

<?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>

以及与我的 map Activity 关联的布局文件:

    <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>

最佳答案

maponMapReady 之前不会初始化,因此在 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);
}
});

关于java - AppCompatActivity 中的 GoogleMap 对象为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457620/

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