Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5933699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:00:36+00:00 2026-05-22T15:00:36+00:00

Ok i have tried Nicholas’s solution but still getting force close if i press

  • 0

Ok i have tried Nicholas’s solution but still getting force close if i press the back button as the app searches for gps fix.so i m uploading all the codes and files(except icon.png and pen.png in drawables) for the project…so that it’s possible just to copy paste the code from here to see whats going on. my main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/icon"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button android:text="Start" 
android:id="@+id/Button01" 
android:layout_width="120px" 
android:layout_height="70px"
android:layout_gravity = "center_horizontal"
android:textSize="20px"
android:layout_marginTop="150px"
android:clickable="true"
></Button>

</LinearLayout>

next my main activity(GpsLocEx.java)

package com.example.gpslocex;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class GpsLocEx extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button start = (Button) findViewById(R.id.Button01);
        start.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
             Intent map = new Intent(view.getContext(), com.example.gpslocex.ShowMap.class);
             startActivityForResult(map, 0);

            }

        });


 }
    }

next my gps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"
        android:apiKey="06ZLf-HkdkRMb513KS2ZRljmMQL0bntlyMJ-rOQ"
    />

</RelativeLayout>

next ShowMap.java

package com.example.gpslocex;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class ShowMap extends MapActivity {

    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    private GeoUpdateHandler geoUpdateHandler;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.gps);
            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
            mapView.setSatellite(true);
            mapController = mapView.getController();
            mapController.setZoom(14); // Zoom 1 is world view
            geoUpdateHandler = new GeoUpdateHandler();

            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                createGpsDisabledAlert();
            } else {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                        0,geoUpdateHandler);

            }

       }
     private void createGpsDisabledAlert() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder
                    .setMessage(
                            "Your GPS is disabled! Would you like to enable it?")
                    .setCancelable(false).setPositiveButton("Enable GPS",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    showGpsOptions();
                                }
                            });
            builder.setNegativeButton("Do nothing",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }

     private void showGpsOptions() {
            Intent gpsOptionsIntent = new Intent(
                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(gpsOptionsIntent);
        } 


     @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }


    public class GeoUpdateHandler implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            mapController.animateTo(point); //  mapController.setCenter(point);
             MapOverlay mapOverlay = new MapOverlay();
              mapOverlay.setPointToDraw(point);
              List<Overlay> listOfOverlays = mapView.getOverlays();
              listOfOverlays.clear();
              listOfOverlays.add(mapOverlay);

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }


    class MapOverlay extends Overlay
    {
      private GeoPoint pointToDraw;

      public void setPointToDraw(GeoPoint point) {
        pointToDraw = point;
      }

      public GeoPoint getPointToDraw() {
        return pointToDraw;
      }

      @Override
      public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);           

        // convert point to pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pen);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 128, null);    
        return true;
      }




    }
    @Override
    public void onPause() {
        locationManager.removeUpdates(geoUpdateHandler);
    }


}

next strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Version-1.0</string>
    <string name="app_name">GpsLocEx</string>

</resources>

Finally AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.gpslocex"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".GpsLocEx"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".ShowMap"></activity>

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

</manifest> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:00:37+00:00Added an answer on May 22, 2026 at 3:00 pm

    You can use the Android lifecycle methods to turn off the GPS when the Activity is no longer on top.

    Use onPause(), onStop(), or onDestroy() to remove updates from your locationManager. Something like this:

    @Override
    public void onPause() {
        locationManager.removeUpdates(myLocationListener);
    }
    

    EDIT:

    You have a ClassCastException in your onPause() method. You cannot cast this (your Activity) to a LocationListener (at least not without making your Activity implement LocationListener).

    In your code, you create a new GeoUpdateHandler(). First, you should create an instanceVariable that is your GeoUpdateHandler. Underneath your instance variable for LocationManager, create one like this:

    private GeoUpdateHandler geoUpdateHandler;
    

    You should keep a handle to this in the onCreate() method:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //...some code here...
        geoUpdateHandler = new GeoUpdateHandler();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                            0,geoUpdateHandler);
        //...maybe some more code here...
    }
    

    And then in the onPause, pass the geoUpdateHandler to the removeUpdates function:

    @Override
    public void onPause() {
        locationManager.removeUpdates(geoUpdateHandler);
    }
    

    EDIT 2:

    The stacktrace says it all:

    android.app.SuperNotCalledException: Activity {com.example.gpslocnav6ex/com.example.gpslocnav6ex.ShowMap} did not call through to super.onPause()
    

    Call through to super.onPause() in your onPause()

    @Override
    public void onPause() {
        super.onPause();
        locationManager.removeUpdates(geoUpdateHandler);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried this... Dim myMatches As String() = System.Text.RegularExpressions.Regex.Split(postRow.Item(Post), \b\#\b) But it is
I have tried to integrate the Picasa API on iPhone, compiles fine, but I
I have tried to find how to create DLL-s on linux using google, but
I have tried different examples to filter a gridview by dropdownlist, but is it
I have tried gaussian blur and checked out all the questions on stackoverflow but
I have tried this but Contacts were not added! ContentResolver cr = this.getContentResolver(); ContentValues
Have tried to compile and run this program, but have received this error message
Have tried various escape functions but can't seem to get the below working... .background-radial(@colour,
I have tried SQLiteConnection(:memory:) and SQLiteConnection(sqlite::memory:) but both of these fail with 'Invalid ConnectionString
I have tried setting it in the code and also in the markup but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.