0

I'm creating an application in which I have to get the user's longitude and latitude and mark his location. My app is giving error "unfortunately Maps has stopped". I googled a lot but unable to find the solution. here is my code and my log cat.

  public class MapView extends Activity {

    private LocationManager locationManager;
    private String best;
    double myLocationLatitude;
    double myLocationLongitude;

    Location loc = locationManager.getLastKnownLocation(best);
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);

        locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        // additionally (you have to implement LocationListener)
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new android.location.LocationListener()
        {

            @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

            }

            @Override
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                 Log.d("a","onLocationChanged: lat=" +location.getLatitude());
                 Log.d("a","onLocationChanged: lat=" +location.getLongitude());
            }
        });

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


    GoogleMap mMap = null;
    mMap.addMarker(new MarkerOptions().draggable(true)
                                      .position(new LatLng(loc.getLatitude(),loc.getLongitude()))
                                      .title("YOUR LOCATION"));

LOG CAT:

03-29 01:37:37.167: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41fb9070noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:37.364: D/dalvikvm(2521): GC_FOR_ALLOC freed 870K, 13% free 6526K/7424K, paused 110ms, total 110ms 03-29 01:37:37.365: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41fc0078noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:37.637: E/linker(2521): load_library(linker.cpp:757): library "/data/data/com.google.android.gms/files/libAppDataSearchExt_armeabi_v7a.so" not found 03-29 01:37:37.642: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41f7ffd8noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:37.833: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41ea8d40noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.324: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41f19948noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.645: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41fa1000noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.658: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41ee72b0noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.668: I/Icing(2521): Internal init done: storage state 0 03-29 01:37:38.725: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@41f7a920noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:38.755: I/Icing(2521): Post-init done 03-29 01:37:38.879: W/asset(2521): AssetManager-->addDefaultAssets CIP path not exsit! 03-29 01:37:40.148: I/dalvikvm(2521): Could not find method android.app.admin.DevicePolicyManager.isDeviceOwnerApp, referenced from method com.google.android.gms.init.InitializeIntentService.onHandleIntent 03-29 01:37:40.148: W/dalvikvm(2521): VFY: unable to resolve virtual method 1697: Landroid/app/admin/DevicePolicyManager;.isDeviceOwnerApp (Ljava/lang/String;)Z 03-29 01:37:40.148: D/dalvikvm(2521): VFY: replacing opcode 0x6e at 0x00e0 03-29 01:37:40.903: I/Icing(2521): Indexing 38E683E5680B78374039C38CC697D19B079F6A63 from com.google.android.googlequicksearchbox 03-29 01:37:41.108: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@420511f0noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:41.505: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@420542a0noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:41.520: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@42055cb8noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:41.534: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@42058ce8noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:41.546: D/ActivityThread(2521): installProvider: context=android.app.ContextImpl@41e4f100holder=android.app.IActivityManager$ContentProviderHolder@4205a700noisy=truenoReleaseNeeded=falsestable=false 03-29 01:37:41.554: I/Icing(2521): Indexing done 38E683E5680B78374039C38CC697D19B079F6A63 03-29 01:37:44.889: D/IPCThreadState(2521): [DN #5] BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x41263ee8

  • You need to initialize locationManager. – Dan Harms Mar 28 '14 at 20:49
  • i tried but the error is not resolving. – Ahmed Talha Mar 28 '14 at 21:28
  • Please update your question with what you have tried. Make sure you are not using `locationManager` before initializing. (`Location loc = locationManager.getLastKnownLocation(best);` is a prime example) – Dan Harms Mar 28 '14 at 21:33
  • You could simply replace the line `private LocationManager locationManager;` with `private LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); ` (but that could create unexpected conflicts) – k_g Mar 28 '14 at 21:38
  • You are also referencing an uninitialized `String best` which will cause problems after you clean up the `locationManager` portion. – Dan Harms Mar 28 '14 at 21:51

2 Answers2

0

Have you added your activity to your android manifest?

I get this error too if I forget to add it into the manifest.

http://developer.android.com/guide/topics/manifest/manifest-intro.html

JonathanSchmied
  • 137
  • 1
  • 7
  • I've already added my activity but still it's giving the error. – Ahmed Talha Mar 28 '14 at 21:27
  • okay... did you add the developer code for google maps into your code? usually i simply get a map without content if the code is not added. Second idea: You have GoogleMap mMap = null, then you try to add something. This does not work in Java Coding – JonathanSchmied Mar 29 '14 at 10:33
  • If you have included Maps in your xmls check out coding like this: GoogleMap map = ((MapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap(); – JonathanSchmied Mar 29 '14 at 10:36
0

You also need to give your app the permission to access the GPS.

Just put the line

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

into your manifest and all will be well.

See also

ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted

Community
  • 1
  • 1
k_g
  • 4,333
  • 2
  • 25
  • 40
  • Then its probably what dcharms mentioned. Since you call the variable outside a method body, it must be initialized outside a method body as well and before it is used. Otherwise, you are referencing a null variable – k_g Mar 28 '14 at 21:46