android - map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap() keeps giving me an error -
i'm beginner @ using maps on android. i'm facing problem when try add following statement code:
map = ((mapfragment) getfragmentmanager().findfragmentbyid(r.id.map)).getmap()
the app keeps crashing when starts up. can please me find solution this?
main.java
package com.example.myfirstapp; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import com.google.android.gms.maps.cameraupdate; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.latlng; public class mainactivity extends actionbaractivity { private final latlng location= new latlng(49.27645, -122.917587); private googlemap map; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); map = ((mapfragment)getfragmentmanager().findfragmentbyid(r.id.map)).getmap(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void onclick_search(view view) { cameraupdate update = cameraupdatefactory.newlatlng(location); map.animatecamera(update); map.setmaptype(googlemap.map_type_satellite); } }
xml file
<relativelayout 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" tools:context="com.example.myfirstapp.mainactivity$placeholderfragment" > <fragment android:id="@+id/map" android:name="com.google.android.gms.maps.mapfragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_below="@+id/search" /> <edittext android:id="@+id/longitude" android:layout_width="110dp" android:layout_height="wrap_content" android:layout_alignbottom="@+id/latitude" android:layout_alignparenttop="true" android:layout_torightof="@+id/latitude" android:ems="10" android:hint="@string/longitude" /> <edittext android:id="@+id/latitude" android:layout_width="110dp" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:ems="10" android:hint="@string/latitude" /> <button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:onclick="onclick_search" android:text="@string/search" /> </relativelayout>
manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="11" android:targetsdkversion="19" /> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices"/> <!-- following 2 permissions not required use google maps android api v2, recommended. --> <uses-permission android:name="android.permission.access_coarse_location"/> <uses-permission android:name="android.permission.access_fine_location"/> <uses-feature android:glesversion="0x00020000" android:required="true"/> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.example.myfirstapp.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="aizasycr9zwp4m9nrknwmfd1jaoec8icski08_c" /> </application> </manifest>
logcat
05-16 12:07:10.600: e/androidruntime(1007): fatal exception: main 05-16 12:07:10.600: e/androidruntime(1007): process: com.example.myfirstapp, pid: 1007 05-16 12:07:10.600: e/androidruntime(1007): java.lang.runtimeexception: unable start activity componentinfo{com.example.myfirstapp/com.example.myfirstapp.mainactivity}: java.lang.nullpointerexception 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread.access$800(activitythread.java:135) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 05-16 12:07:10.600: e/androidruntime(1007): @ android.os.handler.dispatchmessage(handler.java:102) 05-16 12:07:10.600: e/androidruntime(1007): @ android.os.looper.loop(looper.java:136) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread.main(activitythread.java:5017) 05-16 12:07:10.600: e/androidruntime(1007): @ java.lang.reflect.method.invokenative(native method) 05-16 12:07:10.600: e/androidruntime(1007): @ java.lang.reflect.method.invoke(method.java:515) 05-16 12:07:10.600: e/androidruntime(1007): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 05-16 12:07:10.600: e/androidruntime(1007): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 05-16 12:07:10.600: e/androidruntime(1007): @ dalvik.system.nativestart.main(native method) 05-16 12:07:10.600: e/androidruntime(1007): caused by: java.lang.nullpointerexception 05-16 12:07:10.600: e/androidruntime(1007): @ com.example.myfirstapp.mainactivity.oncreate(mainactivity.java:27) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activity.performcreate(activity.java:5231) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 05-16 12:07:10.600: e/androidruntime(1007): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) 05-16 12:07:10.600: e/androidruntime(1007): ... 11 more
this because findviewbyid() searches in activity_main layout, while map located in fragment's layout fragment_main.
move piece of code in oncreateview()
method of fragment
eg:
//... view rootview = inflater.inflate(r.layout.fragment_main, container, false); button buttonclick = (button)rootview.findviewbyid(r.id.button); buttonclick.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { onbuttonclick((button) view); } });
notice access through rootview view:
button buttonclick = (button)rootview.findviewbyid(r.id.button);
otherwise again nullpointerexception.
Comments
Post a Comment