Android 安卓谷歌地图获取地点详细信息并将其发送到其他活动

Android 安卓谷歌地图获取地点详细信息并将其发送到其他活动,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我想将地点详细信息发送到另一个活动,但出现了诸如无法解析方法“getApplication()”、无法解析方法“startActivity”(android.content.Intent)等错误。下面是我迄今为止尝试过的代码 import android.content.Intent; import android.os.AsyncTask; import android.util.Log; import com.google.android.gms.maps.

我想将地点详细信息发送到另一个活动,但出现了诸如无法解析方法“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);
            }
           });

         }
        }


     }
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.util.Log;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.model.BitmapDescriptorFactory;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入java.util.HashMap;
导入java.util.List;
公共类GetNearbyPlacesData扩展异步任务{
字符串googlePlacesData;
谷歌地图;
字符串url;
@凌驾
受保护的字符串doInBackground(对象…参数){
试一试{
Log.d(“GetNearbyPlacesData”,“输入doInBackground”);
mMap=(谷歌地图)参数[0];
url=(字符串)参数[1];
DownloadUrl DownloadUrl=新的DownloadUrl();
googlePlacesData=downloadUrl.readUrl(url);
Log.d(“GooglePlacesReadTask”、“doinbackgroundexit”);
}捕获(例外e){
d(“GooglePlacesReadTask”,例如toString());
}
返回googlePlacesData;
}
@凌驾
受保护的void onPostExecute(字符串结果){
试一试{
Log.d(“GooglePlacesReadTask”,“输入onPostExecute”);
列表nearbyPlacesList=null;
DataParser DataParser=新的DataParser();
nearbyPlacesList=dataParser.parse(结果);
显示临近地点(临近地点列表);
Log.d(“GooglePlacesReadTask”、“onPostExecute退出”);
}捕获(例外e){
e、 printStackTrace();
}
}
私人空间显示近处(列表
近距离放置列表){
for(int i=0;i
您必须使用
GetNearbyPlacesData
类的构造函数

private Context context;

public GetNearbyPlacesData(Context context){
    this.context=new WeakReference<Context>(context);
}
修改
OnInfo WinDoeClick()
,如下所示:

  @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);
    }
   });

希望这能有所帮助。

当我们从外部活动上下文启动活动(i)时,我们必须设置标志

Intent i = new Intent(context, YourNewActivity.class); 
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
 context.startActivity(i); 

getApplication
startActivity
getApplicationContext
都是
Context
类的方法。您正试图从
异步任务
调用它们。请将其包装在
WeakReference
中,以避免内存泄漏。据我所知,
WeakReference
是最后的手段。实际上,这是避免内存泄漏的一种非常常见的模式。在这种情况下,如果传递的
上下文
是一个应用程序上下文,就可以了。如果它是
活动
的上下文,并且
活动
异步任务
运行时被销毁,则GC无法将其从堆中删除,因为异步任务对其上下文具有强引用。这是浪费的堆空间。将其包装在
WeakReference
中,允许GC在需要时将其从堆中删除。
Intent i = new Intent(context, YourNewActivity.class); 
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
 context.startActivity(i);