java - Showing google maps in activity (not MainActivity) -
i'd show google map in android application. found en example tested me putting in main activity. works without problems. improve app i'd outsource map in activity (called displaymapactivity). when button "show map" clicked, map activity called. app crashes , nullpointer exception.
my project example.com.lbsapp2 , here code:
mainactivity.java:
public void openmap(view view) { intent intent = new intent(this, displaymapactivity.class); startactivity(intent); }
this called when clicking on button. should open map.
activity_main.xml:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lbsapp2.mainactivity" tools:ignore="mergerootframe" />
fragment_main.xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/welcome" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="openmap" android:text="@string/button_map" /> </linearlayout>
displaymapactivity.java:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); }
this has worked in mainactivity.java without problems code should okay.
activity_display_map.xml (layout):
<fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.mapfragment"/>
this in activity_main.xml before , has worked fine.
the manifest should okay had worked before.
in displaymapactivity
change this
setcontentview(r.layout.activity_main);
to
setcontentview(r.layout.activity_display_map);
cause mapframgent
in activity_display_map.xml
.
also getmap()
return null. check availability of google play services before initialize googlemap
object
Comments
Post a Comment