1

I have created an action bar navigation tabbed layout and in one of tab i want to display google map and fetching my current location I have used LocationListener to get location but it is returing error here is my code..

package com.example.prototype.tabs;

import java.util.Timer;









import com.example.prototype.MainActivity;
import com.example.prototype.R;
import com.example.prototype.utility.GpsTracker;
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.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class NearByPlacesFragment extends Fragment  implements OnMapReadyCallback, LocationListener {

    GoogleMap gmap;
    Marker mp;
    LatLng ll;
    Location l;
    Context ctx=getActivity();
    double lat,lon;
    boolean isGpsEnabled,isNetworkAvailable,isLocationSet;
    LocationManager lmgr;
    long minTime = 60*1000*1;
    long minDistance = 10;
    Location loc;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.nearbyplaces, container, false);
         l=getLocation();
        if(l==null){Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();}
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

        return rootView;

    }



    @Override
    public void onMapReady(GoogleMap map) {
        // TODO Auto-generated method stub

            map.getUiSettings().setAllGesturesEnabled(true);
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            map.setMyLocationEnabled(true);
            if(l!=null)
            ll = new LatLng(l.getLatitude() , l.getLongitude());
            map.addMarker(new MarkerOptions().position(ll).title("Marker in Sydney"));


    }



     public Location getLocation(){

         lmgr=(LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);


         if(lmgr==null){Toast.makeText(getActivity(), "null manager", Toast.LENGTH_LONG).show();}


         isGpsEnabled=lmgr.isProviderEnabled(LocationManager.GPS_PROVIDER);
         isNetworkAvailable=lmgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

         if(isGpsEnabled==true){
        setLocationManager("gps");
         }
         else if(isNetworkAvailable==true){
             setLocationManager("network");
          }



         return loc;




     }

     private boolean setLocationManager(String s){

         if(s.equals("gps")){

                lmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance, this);
                loc=lmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);        
         }

         else{

                lmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, minDistance, this);
                loc=lmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

         }


         return isLocationSet;
     }



    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub

    }



    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }



    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }



    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }








}

log is :

07-07 00:05:02.424: E/dalvikvm(22824): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
07-07 00:05:02.491: E/dalvikvm(22824): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ij.a
07-07 00:05:02.755: E/AndroidRuntime(22824): FATAL EXCEPTION: main
07-07 00:05:02.755: E/AndroidRuntime(22824): java.lang.NullPointerException
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.example.prototype.tabs.NearByPlacesFragment.getLocation(NearByPlacesFragment.java:92)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.example.prototype.tabs.NearByPlacesFragment.onCreateView(NearByPlacesFragment.java:62)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.example.prototype.MainActivity.onTabSelected(MainActivity.java:172)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1067)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:519)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.view.View.performClick(View.java:4211)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.view.View$PerformClick.run(View.java:17446)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.os.Handler.handleCallback(Handler.java:725)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.os.Looper.loop(Looper.java:153)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at android.app.ActivityThread.main(ActivityThread.java:5297)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-07 00:05:02.755: E/AndroidRuntime(22824):    at dalvik.system.NativeStart.main(Native Method)
07-07 00:05:03.027: E/NativeCrypto(22824): ssl=0x60ac2588 cert_verify_callback x509_store_ctx=0x6151aab0 arg=0x0
07-07 00:05:03.027: E/NativeCrypto(22824): ssl=0x60ac2588 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
AnonymousDev
  • 247
  • 3
  • 15

3 Answers3

0
if(lmgr==null){{Toast.makeText(getActivity(), "null manager", Toast.LENGTH_LONG).show();}

}

You're making a null check, but all it does is pop a Toast. You're still falling through to call isGpsEnabled=lmgr.isProviderEnabled(LocationManager.GPS_PROVIDER);

loadedion
  • 2,217
  • 19
  • 41
  • i have used to check whether it is initalised or not but i dont know why its not initializing ??? – AnonymousDev Jul 06 '15 at 18:58
  • Do you have the location services permissions set in your Manifest file? – loadedion Jul 06 '15 at 18:59
  • yes i have for both coarse and fine as i am using google map to display location – AnonymousDev Jul 06 '15 at 19:00
  • Ah, I see. In that case, the other answer listed by @DanielNugent looks like the correct answer. – loadedion Jul 06 '15 at 19:09
  • Refresh your question, you'll see that you have another response. – loadedion Jul 06 '15 at 19:11
  • @danielNugent i changed it but still there is error – AnonymousDev Jul 06 '15 at 19:16
  • this time error is :FATAL EXCEPTION: main 07-07 00:48:11.703: E/AndroidRuntime(26251): java.lang.IllegalArgumentException: no position in marker options 07-07 00:48:11.703: E/AndroidRuntime(26251): at com.google.l.a.ce.a(Unknown Source) 07-07 00:48:11.703: E/AndroidRuntime(26251): at com.google.maps.api.android.lib6.d.ft.a(Unknown Source) 07-07 00:48:11.703: E/AndroidRuntime(26251): at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167) 07-07 00:48:11.703: E/AndroidRuntime(26251): at android.os.Binder.transact(Binder.java:310) – AnonymousDev Jul 06 '15 at 19:19
  • log says ATAL EXCEPTION: main 07-07 00:56:15.269: E/AndroidRuntime(27848): java.lang.NullPointerException 07-07 00:56:15.269: E/AndroidRuntime(27848): at com.example.prototype.tabs.NearByPlacesFragment.onMapReady(NearByPlacesFragment.java:88) 07-07 00:56:15.269: E/AndroidRuntime(27848): at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) 07-07 00:56:15.269: E/AndroidRuntime(27848): at com.google.android.gms.maps.internal.zzm$zza.onTransact(Unknown Source) – AnonymousDev Jul 06 '15 at 19:27
0

The Main problem is that you're calling getActivity() before it's a valid time to do so:

 Context ctx=getActivity();

Instead, call getActivity() in onCreateView():

public class NearByPlacesFragment extends Fragment  implements OnMapReadyCallback, LocationListener {

    GoogleMap gmap;
    Marker mp;
    LatLng ll;
    Location l;
    Context ctx; //modified
    double lat,lon;
    boolean isGpsEnabled,isNetworkAvailable,isLocationSet;
    LocationManager lmgr;
    long minTime = 60*1000*1;
    long minDistance = 10;
    Location loc;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ctx=getActivity(); //added

        View rootView = inflater.inflate(R.layout.nearbyplaces, container, false);
         l=getLocation();
        if(l==null){Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();}
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);

        return rootView;

    }

You should also make sure that you don't try to place a Marker with a null Location:

@Override
public void onMapReady(GoogleMap map) {
    // TODO Auto-generated method stub

        map.getUiSettings().setAllGesturesEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);

        //added brackets:
        if(loc!=null){
            ll = new LatLng(loc.getLatitude() , loc.getLongitude());
            map.addMarker(new MarkerOptions().position(ll).title("Marker in Sydney"));
        }


}

You should also use your location callback to update loc:

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    loc = location;
    if(loc!=null){
        ll = new LatLng(loc.getLatitude() , loc.getLongitude());
        map.addMarker(new MarkerOptions().position(ll).title("Marker in Sydney"));
    }


}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • still an error log says: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza 07-07 00:48:11.259: E/dalvikvm(26251): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ij.a 07-07 00:48:11.703: E/AndroidRuntime(26251): FATAL EXCEPTION: main 07-07 00:48:11.703: E/AndroidRuntime(26251): java.lang.IllegalArgumentException: no position in marker options 07-07 00:48:11.703: E/AndroidRuntime(26251): at com.google.l.a.ce.a(Unknown Source) – AnonymousDev Jul 06 '15 at 19:23
  • yes it worked but it is returning location returned is still null – AnonymousDev Jul 06 '15 at 19:32
  • its not calling the method on location changed – AnonymousDev Jul 06 '15 at 19:40
  • reason is gps dont work in my mobile its always look for searching so i am more over dependent on network but its give an error also i need on load of fragment it should add a marker of current location – AnonymousDev Jul 06 '15 at 19:50
  • it worked just realised that it is returning null because is network section i have used gps to get location and since gps is off it is reurning null thanx man it worked that 3 rd time u have helped in last couple of days thanx alot..!! :) – AnonymousDev Jul 06 '15 at 19:56
  • @user3508256 No problem! Glad it's working for you now! – Daniel Nugent Jul 06 '15 at 20:00
0

Add the following code to your fragment :

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ctx = activity;
    }

This should fix the Nullpointer Exception.

Regarding the ApOps Issue Refere following links :

android.app.AppOpsManager not find in android project

Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza

Community
  • 1
  • 1
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • still and error Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza 07-07 00:48:11.259: E/dalvikvm(26251): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ij.a 07-07 00:48:11.703: E/AndroidRuntime(26251): FATAL EXCEPTION: main 07-07 00:48:11.703: E/AndroidRuntime(26251): java.lang.IllegalArgumentException: no position in marker options 07-07 00:48:11.703: E/AndroidRuntime(26251): at com.google.l.a.ce.a(Unknown Source) – AnonymousDev Jul 06 '15 at 19:22