0

使用 android 项目,我在自定义覆盖类上遇到问题,我无法从上一个类获取 putextra 值到这个类,它在 getIntent() 方法上显示错误,任何人都可以帮助解决这个错误,这是我的代码..

import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * Class used to place marker or any overlay items on Map
 * */
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {

       private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

       private Context context;
       String place_reference;

       public AddItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
       }

       public AddItemizedOverlay(Drawable defaultMarker, Context context) {
            this(defaultMarker);
            this.context = context;
       }

       @Override
       public boolean onTouchEvent(MotionEvent event, MapView mapView)
       {   

           if (event.getAction() == 1) {
               GeoPoint geopoint = mapView.getProjection().fromPixels(
                   (int) event.getX(),
                   (int) event.getY());
               // latitude
               double lat = geopoint.getLatitudeE6() / 1E6;
               // longitude
               double lon = geopoint.getLongitudeE6() / 1E6;
               //Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
           }
           return false;
       } 

       @Override
       protected OverlayItem createItem(int i) {
          return mapOverlays.get(i);
       }

       @Override
       public int size() {
          return mapOverlays.size();
       }

       @Override
       protected boolean onTap(int index) {
         OverlayItem item = mapOverlays.get(index);
         AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
         dialog.setTitle(item.getTitle());
         dialog.setMessage(item.getSnippet());
         dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Intent i = getIntent();
                i.getStringExtra(place_reference);

                Intent intent = new Intent(context, SinglePlaceActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent );
            }
         })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
//                  MainActivity.this.finish();
                }
            });
         dialog.show();
         return true;
       }

       public void addOverlay(OverlayItem overlay) {
          mapOverlays.add(overlay);
       }

       public void populateNow(){
           this.populate();
       }

    }

这是我在代码中发现的错误

在此处输入图像描述

4

4 回答 4

1

getIntent()是一种方法Activity。在你的情况下,我会让构造函数像这样:

private Activity activity;

public AddItemizedOverlay(Drawable defaultMarker, Context context, Activity a) {
    this(defaultMarker);
    this.context = context;
    this.activity = a;
}

然后使用activity.getIntent();

于 2012-10-18T10:11:01.493 回答
0

写得像 Intent i= context.getIntent();

因为你的班级持有这个意图是一个内部班级。

于 2012-10-18T11:18:57.993 回答
0

您需要将 Activty 传递给您的 AddItemizedOverlay。然后你可以打电话

activity.getIntent();
于 2012-10-18T10:13:34.720 回答
0

我认为最好将您传递place_reference给构造函数并将其保存为实例变量,而不是整个活动。显然,当你只需要一个额外的字符串时,这会更好,当它在这个类中只使用一次时也是如此。你可以试试:

String place_reference;    

public AddItemizedOverlay(Drawable defaultMarker, String place_reference) {
this(defaultMarker);
this.place_reference = place_reference;
}
// and then use place_reference any where you need
于 2012-10-18T10:17:12.663 回答