在我的 android.Map 应用程序中,我使用ShowTheatersTask
类在地图上显示附近的剧院,并且在该剧院的 Tap 上我必须显示方向。我正在 HelloItemizedOverlay 类中创建 WebViewDemo 的 Obj。而我正在访问showDrections()
修改后的全局变量在 ShowTheatersTaskdoInBackground()
中显示空值。甚至current_latitude
,current_langitude
作为0.0
,它们被初始化,onCreate()
其中是全局变量。请帮助我为什么得到NullPointerException
。这里是代码的主要部分。
注意:在onTap(int index)
类中HelloItemizedOverlay
我正在创建 obj
WebViewDemo.java
public class WebViewDemo extends MapActivity {
public double current_latitude, current_langitude;
public String lat = null, lng = null;
ShowTheatersTask showTheatersTask;
private LocationManager locationManager;
public void onCreate(Bundle savedInstanceState) {
.....
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
Location location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
dialog = ProgressDialog.show(WebViewDemo.this, "Please wait",
"Theaters are loading...", true);
dialog.show();
current_latitude = location.getLatitude();
current_langitude = location.getLongitude();
.......
showTheatersTask = new ShowTheatersTask();
showTheatersTask.execute(current_latitude, current_langitude);
}
class ShowTheatersTask extends AsyncTask<Double, Void, Void> {
@Override
protected Void doInBackground(Double... params) {
double latitude = params[0], langitude = params[1];
try {
httpClient = new DefaultHttpClient();// Client
postRequest = new HttpPost(..web service call..);
response = httpClient.execute(postRequest);
//Sure getting responne as JSOn object
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonData = jsonArray.getJSONObject(i);
String name = jsonData.getString("name");
JSONObject locObject = jsonData.getJSONObject("geometry")
.getJSONObject("location");
lat = locObject.getString("lat");
lng = locObject.getString("lng");
point = new GeoPoint((int) (Double.parseDouble(lat) * 1E6),
(int) (Double.parseDouble(lng) * 1E6));
overlayitem = new MyOverLayItem(point, name,
jsonData.getString("vicinity"), null);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
mapController.animateTo(point);
mapView.postInvalidate();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void showDrections() {
/*****************
*Here values are coming null
*****************/
try {
Log.d("current lat lng values", current_latitude+" "+current_langitude);
Log.d("lat lng values", lat+" "+lng);
}
HelloItemizedOverlay.java
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
//some methods
@Override
protected boolean onTap(int index) {
WebViewDemo.MyOverLayItem item = (WebViewDemo.MyOverLayItem) mOverlays
.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Get directions", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
WebViewDemo demo=new WebViewDemo();
demo.showDrections();
}
});
dialog.show();
return true;
}
}