hi,
i am developing a google maps application for android based mobile
devices..
i trie loading the map but it is returning a null pointer exception..
i am pasting my code here..
package com.google.MapsProject;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MapsProject extends Activity implements LocationListener{
/** Called when the activity is first created. */
WebView webView;
private Location mostRecentLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getLocation();
setupWebView();
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
public void getLocation(){
LocationManager lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
String provider = lm.getBestProvider(c,true);
lm.requestLocationUpdates(provider, 1, 0, this);
mostRecentLocation = lm.getLastKnownLocation(provider);
}
public void setupWebView(){
final String centerURL = "javascript:centerAt(" +
mostRecentLocation.getLatitude() + "," +
mostRecentLocation.getLongitude()+ ")";
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
//Wait for the page to load then send the location information
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("file:///android_asset/localmap.html");
/** Allows JavaScript calls to access application resources **/
webView.setWebChromeClient(new WebChromeClient());
webView.addJavascriptInterface(new JavaScriptInterface(),
"android");
}
/** Sets up the interface for getting access to Latitude and
Longitude data from device
**/
final class JavaScriptInterface {
public double getLatitude(){
return mostRecentLocation.getLatitude();
}
public double getLongitude(){
return mostRecentLocation.getLongitude();
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@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
}
}
local.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://
www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Hello Maps</title>
<script type = "text/javascript" src = "
http://maps.google.com/maps/
api/js?sensor=true"></script>
<script type="text/javascript">
var map;
function initialize(){
var latitude = 0;
var longitude = 0;
if (
window.android){
latitude = window.android.getLatitude();
longitude = window.android.getLongitude();
}
var myLatlng = new google.maps.LatLng(latitude,longitude);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
function centerAt(latitude,longitude){
myLatlng = new google.maps.LatLng(latitude,longitude);
map.panTo(myLatlng);
}
</script>
</head>
<body>
</body>
</html>
can anyone please tell me why it is throwing a null pointer exception
and what should i do to remove it???