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

The Archive Base Latest Questions

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

I’m fairly new to developing in Android and I’m not sure how to solve

  • 0

I’m fairly new to developing in Android and I’m not sure how to solve this problem. I released my app and started to get reports from some people with Android 2.1 that it crashes. Worked fine in my HTC Desire 2.2 and seems to work in simulated 1.6

and it does crashes in simulated 2.1 if I do not quickly send some gps with eclipse DDMS.

And it do not crash if I change some code not to ask for gps position…

I search google a lot on it and it seems the error is that "getLastKnownLocatio" sometimes returns null and that makes it crash, but I can’t find a workaround for that to fit in my code…. I’m not the best programmer in the world.

Here is my java:

public class WebPageLoader extends Activity implements LocationListener{
public static String Android_ID = null;
final Activity activity = this;
private Location mostRecentLocation;



private void getLocation() {
    
    LocationManager locationManager =
      (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria,true);
    //In order to make sure the device is getting the location, request updates.
    //locationManager.requestLocationUpdates(provider, 10, 1, this);

    //mostRecentLocation = locationManager.getLastKnownLocation(provider);
    locationManager.requestLocationUpdates(provider, 1000, 500, this);      
    mostRecentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  }

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);
    //getLocation();
    Android_ID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
    WebView webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    /** Allows JavaScript calls to access application resources **/
    webView.addJavascriptInterface(new JavaScriptInterface(), "android16");
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {
            activity.setTitle("Letar poliskontroller");
            activity.setProgress(progress * 100);

            if(progress == 100)
                activity.setTitle(R.string.app_name);
        }
    });
    getLocation();

    


    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        }

        
        
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });

    webView.loadUrl("http://m.domain.se/android/file.php");
}
    /** Sets up the interface for getting access to Latitude and Longitude data from device
     **/




private class JavaScriptInterface {

    public double getLatitude(){
      return mostRecentLocation.getLatitude();
    }
    public double getLongitude(){
      return mostRecentLocation.getLongitude();
    }
    
    public String getAndroid_ID(){
        return Android_ID;
    }
  }


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

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

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

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuItem item = menu.add("Meny");
    item = menu.add("Stäng app");
    item.setIcon(R.drawable.exit);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (item.getTitle() == "Stäng app") {
        finish();
    }
    return true;
}
}

UPDATE

With your suggestion for the javacall which prevents the crashing, I also added this in the java of the receiving Google map

  latitude = window.android16.getLatitude();
  longitude = window.android16.getLongitude();
  
  if ( isNaN(latitude) ) {
      latitude = 61.72680992165949;
  } else {
      latitude = latitude;
  }
  
      if ( isNaN(longitude) ) {
      longitude = 17.10124969482422;
  } else {
      longitude = longitude;
  }

Now it seems to work… Cheers and thanks a lot, I’ll try this some and get back to report when I bug tested it some.

  • 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-18T07:36:23+00:00Added an answer on May 18, 2026 at 7:36 am

    LocationManager.getLastKnownLocation() returns the last known location for the provider, or null. So in your JavaScriptInterface you should also check for a null in order not to get a NullPointerException.

    UPDATE:

    private class JavaScriptInterface {
    
        public double getLatitude(){
            return mostRecentLocation != null ? mostRecentLocation.getLatitude() : Double.NaN;
        }
    
        public double getLongitude(){
            return mostRecentLocation != null ? mostRecentLocation.getLongitude() : Double.NaN;
        }
    
        public String getAndroid_ID(){
            return Android_ID;
        }
    }
    

    Also your javascript should be fixed to be able to process Double.NaN as an indication of location unavailability. You may also use some other constant instead of Double.NaN to represent that state, it should just be out of the range of valid lon/lat values.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from

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.