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 7040565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:01+00:00 2026-05-28T01:56:01+00:00

Hai am new in android Application,am trying to get the latitude and longtitude using

  • 0

Hai am new in android Application,am trying to get the latitude and longtitude using Broadcast receiver.But my Application is Getting force Close.I dont know why it happens.Anybody kindly rectify my error.

        @Override
        public void onClick(View v) 
        {
             locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
             // Define the criteria how to select the locatioin provider -> use
             // default
             Criteria criteria = new Criteria();

         provider = locationManager.getBestProvider(criteria, false);
         Location location = locationManager.getLastKnownLocation(provider);

         // Initialize the location fields
         if (location != null) {
            System.out.println("Provider " + provider + " has been selected.");
            float lat = (float) (location.getLatitude());
            float lng = (float) (location.getLongitude());
            latituteField.setText(String.valueOf(lat));
            longitudeField.setText(String.valueOf(lng));

         } else {
            latituteField.setText("provider not available");
            longitudeField.setText("provider not available");
         }
            registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    });

        private class ConnectivityReceiver extends BroadcastReceiver{

            @Override
            public void onReceive(Context context, Intent intent) {
                    NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);

                    if(null != info)
                    {
                            String state = getNetworkStateString(info.getState());
                            String stateString = info.toString().replace(',', '\n');
                            if(state.equals("Connected")){
                                result();

                            }

                    }
            }

}
    @Override
    protected void onResume() {
        super.onResume();
        //locationManager.requestLocationUpdates(provider,1000, 1, this);
        locationManager.requestLocationUpdates(
                provider, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                this
        );
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        float lat = (float) (location.getLatitude());
        float lng = (float) (location.getLongitude());
        longitudeField.setText(String.valueOf(lng));
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Disenabled provider " + provider,
                Toast.LENGTH_SHORT).show();

    }
    protected void Display(Cursor c) {
        Toast.makeText(this, "rowid: " + c.getString(0) + "\n" +
         "Latitude: " + c.getString(1) + "\n" + "Longitude: " + c.getString(2) + "\n" +
         Toast.LENGTH_LONG, 0).show();    
    }

    **updata Logcat Error Message**



   "01-06 12:08:27.376: ERROR/AndroidRuntime(9725): java.lang.RuntimeException: Unable to resume activity {com.varma.samples.conntest/com.varma.samples.conntest.MainActivity}: java.lang.NullPointerException
    01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at  android.app.ActivityThread.performResumeActivity(ActivityThread.java:3133)
    01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3148)
    01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2689)
    01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.os.Handler.dispatchMessage(Handler.java:99)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.os.Looper.loop(Looper.java:123)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread.main(ActivityThread.java:4632)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at java.lang.reflect.Method.invokeNative(Native Method)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at java.lang.reflect.Method.invoke(Method.java:521)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at dalvik.system.NativeStart.main(Native Method)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725): Caused by: java.lang.NullPointerException
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at com.varma.samples.conntest.MainActivity.onResume(MainActivity.java:158)
     01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
    01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.Activity.performResume(Activity.java:3823)
  01-06 12:08:27.376: ERROR/AndroidRuntime(9725):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3123)
"

my manifestfile*update*

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.varma.samples.conntest"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".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>

</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_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-28T01:56:01+00:00Added an answer on May 28, 2026 at 1:56 am

    Move the line

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

    from onclick method to oncreate method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hai am trying to get the gps location using BroadCast Receiver.its working fine,But i
Hai Guys, How to get wan ip address of my user using php....
Hai i developed a application in android,in my application i used a dynamically created
Hai am trying to set alarm for every 10 minutes.But its running first time
Hai guys, I started a new asp.net mvc application and i followed the steps
I am trying to implement Camera application in android,and i got some code from
hai i am very new to jquery and jqgrid.. i have implemented a grid...but
Hai i have a application which is try to load the swf,but i cannot
Hai i treid to access the latitude and longtitude value in background service in
hai every one. i am new to android. in my project i had some

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.