android.i の私のマップ アプリでは、ShowTheatersTask
クラスを使用して近くの劇場を地図上に表示し、その劇場のタップで方向を表示する必要があります。HelloItemizedOverlay クラスで WebViewDemo の Obj を作成していますshowDrections()
。変更したグローバル変数にアクセスしていますShowTheatersTaskでは、doInBackground()
null 値が表示されています。グローバル変数で初期化されているので、コードの主要部分です。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;
}
}