我写了一个程序来查找附近的地方,并且这些地方用标记显示完美,当我点击标记时,infowindow显示详细信息,但我想显示在infowindow中出现的地方详细信息新活动,并在此参考的帮助下,我已正确遵循这些步骤。 引用的链接:http://wptrafficanalyzer.in/blog/showing-nearby-places-and-place-details-using-google-places-api-and-google-maps-android-api-v2/#comment-751486
但是我在PlaceDetailsActivity类的onPostExecute方法的第一行得到一个空指针异常,这是我在显示infowindow详细信息的新活动。我无法弄清问题是什么,需要一些帮助
也在这里发布代码:
PlaceDetailsActivity:在onPostEcecute的第一行获取错误
public class PlaceDetailsActivity extends Activity {
WebView mWvPlaceDetails;
private static final String BROWSER_API_KEY = "AIzaSyDcFG73vwdcSrEDdOSVWavV71eqeGIwjXY";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_details);
// Getting reference to WebView ( wv_place_details ) of the layout
// activity_place_details
mWvPlaceDetails = (WebView) findViewById(R.id.wv_place_details);
mWvPlaceDetails.getSettings().setUseWideViewPort(false);
// Getting place reference from the map
String reference = getIntent().getStringExtra("reference");
StringBuilder sb = new StringBuilder(
"https://maps.googleapis.com/maps/api/place/details/json?");
sb.append("reference=" + reference);
sb.append("&sensor=true");
sb.append("&key=" + BROWSER_API_KEY);
// Creating a new non-ui thread task to download Google place details
PlacesTask placesTask = new PlacesTask();
// Invokes the "doInBackground()" method of the class PlaceTask
placesTask.execute(sb.toString());
};
/** A method to download json data from url */
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
/** A class, to download Google Place Details */
private class PlacesTask extends AsyncTask<String, Integer, String> {
String data = null;
// Invoked by execute() method of this object
@Override
protected String doInBackground(String... url) {
try {
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(String result) {
ParserTask parserTask = new ParserTask();
// Start parsing the Google place details in JSON format
// Invokes the "doInBackground()" method of the class ParseTask
parserTask.execute(result);
}
}
/** A class to parse the Google Place Details in JSON format */
private class ParserTask extends
AsyncTask<String, Integer, HashMap<String, String>> {
JSONObject jObject;
// Invoked by execute() method of this object
@Override
protected HashMap<String, String> doInBackground(String... jsonData) {
HashMap<String, String> hPlaceDetails = null;
PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();
try {
jObject = new JSONObject(jsonData[0]);
// Start parsing Google place details in JSON format
hPlaceDetails = placeDetailsJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
return hPlaceDetails;
}
// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(HashMap<String, String> hPlaceDetails) {
String name = hPlaceDetails.get("name");
String icon = hPlaceDetails.get("icon");
String vicinity = hPlaceDetails.get("vicinity");
String lat = hPlaceDetails.get("lat");
String lng = hPlaceDetails.get("lng");
String formatted_address = hPlaceDetails.get("formatted_address");
String formatted_phone_number = hPlaceDetails
.get("formatted_phone_number");
String website = hPlaceDetails.get("website");
String rating = hPlaceDetails.get("rating");
String international_phone_number = hPlaceDetails
.get("international_phone_number");
String url = hPlaceDetails.get("url");
String mimeType = "text/html";
String encoding = "utf-8";
String data = "<html>" + "<body><img style='float:left' src="
+ icon + " /><h1><center>" + name + "</center></h1>"
+ "<br style='clear:both' />" + "<hr />" + "<p>Vicinity : "
+ vicinity + "</p>" + "<p>Location : " + lat + "," + lng
+ "</p>" + "<p>Address : " + formatted_address + "</p>"
+ "<p>Phone : " + formatted_phone_number + "</p>"
+ "<p>Website : " + website + "</p>" + "<p>Rating : "
+ rating + "</p>" + "<p>International Phone : "
+ international_phone_number + "</p>"
+ "<p>URL : <a href='" + url + "'>" + url + "</p>"
+ "</body></html>";
// Setting the data in WebView
mWvPlaceDetails.loadDataWithBaseURL("", data, mimeType, encoding,
"");
}
}
}
PlaceDetailsJSONParser:
public class PlaceDetailsJSONParser {
/** Receives a JSONObject and returns a list */
public HashMap<String, String> parse(JSONObject jObject) {
JSONObject jPlaceDetails = null;
try {
/** Retrieves all the elements in the 'places' array */
jPlaceDetails = jObject.getJSONObject("result");
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Invoking getPlaces with the array of json object where each json
* object represent a place
*/
return getPlaceDetails(jPlaceDetails);
}
/** Parsing the Place Details Object object */
private HashMap<String, String> getPlaceDetails(JSONObject jPlaceDetails) {
HashMap<String, String> hPlaceDetails = new HashMap<String, String>();
String name = "-NA-";
String icon = "-NA-";
String vicinity = "-NA-";
String latitude = "";
String longitude = "";
String formatted_address = "-NA-";
String formatted_phone_number = "-NA-";
String website = "-NA-";
String rating = "-NA-";
String international_phone_number = "-NA-";
String url = "-NA-";
try {
// Extracting Place name, if available
if (!jPlaceDetails.isNull("name")) {
name = jPlaceDetails.getString("name");
}
// Extracting Icon, if available
if (!jPlaceDetails.isNull("icon")) {
icon = jPlaceDetails.getString("icon");
}
// Extracting Place Vicinity, if available
if (!jPlaceDetails.isNull("vicinity")) {
vicinity = jPlaceDetails.getString("vicinity");
}
// Extracting Place formatted_address, if available
if (!jPlaceDetails.isNull("formatted_address")) {
formatted_address = jPlaceDetails
.getString("formatted_address");
}
// Extracting Place formatted_phone, if available
if (!jPlaceDetails.isNull("formatted_phone_number")) {
formatted_phone_number = jPlaceDetails
.getString("formatted_phone_number");
}
// Extracting website, if available
if (!jPlaceDetails.isNull("website")) {
website = jPlaceDetails.getString("website");
}
// Extracting rating, if available
if (!jPlaceDetails.isNull("rating")) {
rating = jPlaceDetails.getString("rating");
}
// Extracting rating, if available
if (!jPlaceDetails.isNull("international_phone_number")) {
international_phone_number = jPlaceDetails
.getString("international_phone_number");
}
// Extracting url, if available
if (!jPlaceDetails.isNull("url")) {
url = jPlaceDetails.getString("url");
}
latitude = jPlaceDetails.getJSONObject("geometry")
.getJSONObject("location").getString("lat");
longitude = jPlaceDetails.getJSONObject("geometry")
.getJSONObject("location").getString("lng");
hPlaceDetails.put("name", name);
hPlaceDetails.put("icon", icon);
hPlaceDetails.put("vicinity", vicinity);
hPlaceDetails.put("lat", latitude);
hPlaceDetails.put("lng", longitude);
hPlaceDetails.put("formatted_address", formatted_address);
hPlaceDetails.put("formatted_phone_number", formatted_phone_number);
hPlaceDetails.put("website", website);
hPlaceDetails.put("rating", rating);
hPlaceDetails.put("international_phone_number",
international_phone_number);
hPlaceDetails.put("url", url);
} catch (JSONException e) {
e.printStackTrace();
}
return hPlaceDetails;
}
}
LogCat错误
12-26 22:17:47.896: E/AndroidRuntime(9806): FATAL EXCEPTION: main
12-26 22:17:47.896: E/AndroidRuntime(9806): java.lang.NullPointerException
12-26 22:17:47.896: E/AndroidRuntime(9806): at com.android.d_governance.PlaceDetailsActivity$ParserTask.onPostExecute(PlaceDetailsActivity.java:148)
12-26 22:17:47.896: E/AndroidRuntime(9806): at com.android.d_governance.PlaceDetailsActivity$ParserTask.onPostExecute(PlaceDetailsActivity.java:1)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.os.Looper.loop(Looper.java:158)
12-26 22:17:47.896: E/AndroidRuntime(9806): at android.app.ActivityThread.main(ActivityThread.java:5751)
12-26 22:17:47.896: E/AndroidRuntime(9806): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 22:17:47.896: E/AndroidRuntime(9806): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 22:17:47.896: E/AndroidRuntime(9806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
12-26 22:17:47.896: E/AndroidRuntime(9806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
12-26 22:17:47.896: E/AndroidRuntime(9806): at dalvik.system.NativeStart.main(Native Method)