java - GoogleMap object is null in AppCompatActivity -
this question has answer here:
i have activity want display map. thus, use mapview along googlemap object so. however, got null object error in maps activity when trying change map type of map. have read topics on such problem problem persists.
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 display further display clicked place map_view = (mapview)findviewbyid(r.id.mapview); map_view.oncreate(savedinstancestate); // gets googlemap mapview , 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(); } }
here manifest:
<?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>
and layout file, associated maps 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>
map
never initialized until onmapready
, therefore other usages of in oncreate
cause nullpointerexception.
you can try this
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); } });
Comments
Post a Comment