Note that there are some explanatory texts on larger screens.

plurals
  1. POgetLastKnownLocation crashes android 2.1
    text
    copied!<p>im fairly new to developing in android and im not shure how to solve this problem. I released my app and started to get reports from some ppl with android 2.1 that it crashes. Worked fine in my HTC Desire 2.2 and seems to work in simulated 1.6</p> <p>and it does crashes in simulated 2.1 if i do not quickly send some gps with eclipse DDMS.</p> <p>And it do not crash if i change some code not to ask for gps position... </p> <p>I search google alot on it and it seems the error is that "getLastKnownLocatio" sometimes retrns null and that makes it crash, but i cant find a workaround for that to fit in my code.... im not the best programmer in the world :)</p> <p>Please help me...</p> <p>Here is my java:</p> <pre><code>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; } </code></pre> <p>}</p> <p><strong>UPDATE</strong> </p> <p>With your sugestion for the javacall wich prevents the crashing, I also added this in the java of the reciving google map </p> <pre><code> 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; } </code></pre> <p>Now it seems to work... Cheers and thanks alot, ill try this some and get back to report when i bugtested it some. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload