开发者

getLastKnownLocation crashes Android 2.1

开发者 https://www.devze.com 2023-01-27 13:21 出处:网络
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

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 o开发者_如何学编程nProviderEnabled(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.


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.


Try something like this although returning 0 by default (if mostRecentLocation is null) may not be what you want - not sure what else to do as you have to return something.

private class JavaScriptInterface {

      public double getLatitude(){
        if (mostRecentLocation != null) // Check for null
              return mostRecentLocation.getLatitude();
        else
              return (Double) 0;
      }
      public double getLongitude(){
        if (mostRecentLocation != null) //Check for null
              return mostRecentLocation.getLongitude();
        else
              return (Double) 0;
      }

      public String getAndroid_ID(){
          return Android_ID;
      }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消