android - Error in Google Map API inside Navigation Bar Fragments -


i trying build app navigation bar , google map api in it.

i followed both of these:

http://www.androidhive.info/2015/04/android-getting-started-with-material-design/

http://blog.teamtreehouse.com/beginners-guide-location-android

tutorial , when put map tutorial 1 of fragment inside navigation bar i'm getting these error:

mygoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build(); 

the error in "this" inside builder.

this complete class code:

package com.clark.androbotics.projectapp;  import android.app.activity; import android.content.intentsender; import android.location.location; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.supportmapfragment; import com.google.android.gms.maps.uisettings; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.markeroptions;  public class mapfragment extends fragment implements     googleapiclient.connectioncallbacks,     googleapiclient.onconnectionfailedlistener,     locationlistener {  public static final string tag = mapfragment.class.getsimplename();  private final static int connection_failure_resolution_request = 9000;  private googleapiclient mygoogleapiclient; private locationrequest mylocationrequest;  private googlemap mmap; // might null if google play services apk not available.  public mapfragment() {     // required empty public constructor }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setupmapifneeded();      mygoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build();      // create locationrequest object     mylocationrequest = locationrequest.create()             .setpriority(locationrequest.priority_high_accuracy)             .setinterval(10 * 1000)         // 10 seconds, in milliseconds             .setfastestinterval(1 * 1000);  // 1 second, in milliseconds  }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.fragment_map, container, false);      // inflate layout fragment     return rootview; }  @override public void onresume() {     super.onresume();     setupmapifneeded();      mygoogleapiclient.connect(); }  @override public void onpause() {     super.onpause();     if (mygoogleapiclient.isconnected()) {         locationservices.fusedlocationapi.removelocationupdates(mygoogleapiclient, this);         mygoogleapiclient.disconnect();     } }  /**  * sets map if possible (i.e., google play services apk correctly  * installed) , map has not been instantiated.. ensure ever  * call {@link #setupmap()} once when {@link #mmap} not null.  * <p/>  * if isn't installed {@link com.google.android.gms.maps.supportmapfragment} (and  * {@link com.google.android.gms.maps.mapview mapview}) show prompt user  * install/update google play services apk on device.  * <p/>  * user can return fragmentactivity after following prompt , correctly  * installing/updating/enabling google play services. since fragmentactivity may not  * have been destroyed during process (it  * stopped or paused), {@link #oncreate(bundle)} may not called again should call  * method in {@link #onresume()} guarantee called.  */ private void setupmapifneeded() {     // null check confirm have not instantiated map.     if (mmap == null) {         // try obtain map supportmapfragment.         mmap = ((supportmapfragment) getfragmentmanager().findfragmentbyid(r.id.map))                 .getmap();         // check if successful in obtaining map.         if (mmap != null) {             setupmap();         }     } }  /**  * can add markers or lines, add listeners or move camera. in case,  * add marker near africa.  * <p/>  * should called once , when sure {@link #mmap} not null.  */ private void setupmap() {     uisettings mapsettings;     mapsettings = mmap.getuisettings();      mapsettings.setzoomcontrolsenabled(true);      mmap.setmaptype(googlemap.map_type_hybrid); }  private void handlenewlocation(location mylocation) {     double currentlatitude = mylocation.getlatitude();     double currentlongitude = mylocation.getlongitude();     latlng latlng = new latlng(currentlatitude, currentlongitude);      markeroptions options = new markeroptions().position(latlng).title("i here!");     mmap.addmarker(options);     mmap.movecamera(cameraupdatefactory.newlatlngzoom(latlng, 17)); }  @override public void onattach(activity activity) {     super.onattach(activity); }  @override public void ondetach() {     super.ondetach(); }  @override public void onconnected(bundle bundle) {     location mylocation = locationservices.fusedlocationapi.getlastlocation(mygoogleapiclient);      if (mylocation == null) {         locationservices.fusedlocationapi.requestlocationupdates(mygoogleapiclient, mylocationrequest, this);     }     else {         handlenewlocation(mylocation);     }; }  @override public void onconnectionsuspended(int i) {  }  @override public void onlocationchanged(location location) {  }  @override public void onconnectionfailed(connectionresult connectionresult) {     if (connectionresult.hasresolution()) {         try {             // start activity tries resolve error             connectionresult.startresolutionforresult(this, connection_failure_resolution_request);         } catch (intentsender.sendintentexception e) {             e.printstacktrace();         }     } else {         log.i(tag, "location services connection failed code " + connectionresult.geterrorcode());     }  } } 

i believe added necessary imports, can't seem fix it.

i'm new android development, i'm not quite sure seems problem.

edit: app runs force closed whenever go fragment.

seems there's error null object reference.

here's logcat:

    05-08 15:42:20.468  24080-24080/com.clark.androbotics.projectapp e/androidruntime﹕ fatal exception: main process: com.clark.androbotics.projectapp, pid: 24080 java.lang.nullpointerexception: attempt invoke virtual method 'com.google.android.gms.maps.googlemap com.google.android.gms.maps.supportmapfragment.getmap()' on null object reference         @ com.clark.androbotics.projectapp.mapfragment.setupmapifneeded(mapfragment.java:108)         @ com.clark.androbotics.projectapp.mapfragment.oncreate(mapfragment.java:46)         @ android.support.v4.app.fragment.performcreate(fragment.java:1763)         @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:915)         @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136)         @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739)         @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499)         @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:456)         @ android.os.handler.handlecallback(handler.java:739)         @ android.os.handler.dispatchmessage(handler.java:95)         @ android.os.looper.loop(looper.java:135)         @ android.app.activitythread.main(activitythread.java:5293)         @ java.lang.reflect.method.invoke(native method)         @ java.lang.reflect.method.invoke(method.java:372)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:904)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:699) 

i've believe .getmap on fragment, i'm not sure. i've tried putting in oncreateview, still have same error.

any appreciated.

the googleapiclient.builder constructor takes context such activity, service, etc, passing this in case fragment (which doesn't extend context).

instead, pass in getactivity(), returns activity fragment attached to:

mygoogleapiclient = new googleapiclient.builder(getactivity())         .addconnectioncallbacks(this)         .addonconnectionfailedlistener(this)         .addapi(locationservices.api)         .build(); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -