我想将地方详细信息发送到另一个活动,但我收到错误,如无法解析methods'getApplication(),无法解析方法'startActivity(android.content.Intent)。下面是我到目前为止尝试过的代码。
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.HashMap;
import java.util.List;
public class GetNearbyPlacesData extends AsyncTask<Object, String,
String> {
String googlePlacesData;
GoogleMap mMap;
String url;
@Override
protected String doInBackground(Object... params) {
try {
Log.d("GetNearbyPlacesData", "doInBackground entered");
mMap = (GoogleMap) params[0];
url = (String) params[1];
DownloadUrl downloadUrl = new DownloadUrl();
googlePlacesData = downloadUrl.readUrl(url);
Log.d("GooglePlacesReadTask", "doInBackground Exit");
} catch (Exception e) {
Log.d("GooglePlacesReadTask", e.toString());
}
return googlePlacesData;
}
@Override
protected void onPostExecute(String result) {
try {
Log.d("GooglePlacesReadTask", "onPostExecute Entered");
List<HashMap<String, String>> nearbyPlacesList = null;
DataParser dataParser = new DataParser();
nearbyPlacesList = dataParser.parse(result);
ShowNearbyPlaces(nearbyPlacesList);
Log.d("GooglePlacesReadTask", "onPostExecute Exit");
} catch (Exception e) {
e.printStackTrace();
}
}
private void ShowNearbyPlaces(List<HashMap<String, String>>
nearbyPlacesList) {
for (int i = 0; i < nearbyPlacesList.size(); i++) {
Log.d("onPostExecute","Entered into showing locations");
MarkerOptions markerOptions = new MarkerOptions();
HashMap<String, String> googlePlace = nearbyPlacesList.get(i);
double lat = Double.parseDouble(googlePlace.get("lat"));
double lng = Double.parseDouble(googlePlace.get("lng"));
final String placeName = googlePlace.get("place_name");
final String vicinity = googlePlace.get("vicinity");
String phone = googlePlace.get("phone_number");
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName + " : " + vicinity + phone);
mMap.addMarker(markerOptions);
markerOptions.icon
(BitmapDescriptorFactory.defaultMarker(BitmapDescriptor
Factory.HUE_RED));
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
// Called when the user clicks a marker of any place.
mMap.setOnInfoWindowClickListener(new
GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent i = new Intent(getApplication(),
Placedetails.class);
//sends this to the next activity
i.putExtra("placename", placeName);
i.putExtra("vici", vicinity);
i.putExtra("title", marker.getTitle());
startActivity(i);
}
});
}
}
}
答案 0 :(得分:1)
您必须使用GetNearbyPlacesData
课程的构造函数。
private Context context;
public GetNearbyPlacesData(Context context){
this.context=new WeakReference<Context>(context);
}
然后从相应的活动传递您的活动的上下文,如下所示:
new GetNearbyPlacesData(this).execute();
修改 onInfoWindoeClick()
,如下所示:
@Override
public void onInfoWindowClick(Marker marker) {
Intent i = new Intent(context, Placedetails.class); //sends this to the next activity
i.putExtra("placename", placeName);
i.putExtra("vici", vicinity);
i.putExtra("title", marker.getTitle());
context.startActivity(i);
}
});
希望这有帮助。
答案 1 :(得分:0)
当我们从外部活动上下文startActivity(i)
时,我们必须设置标志。
Intent i = new Intent(context, YourNewActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);