在overlay类中获取intent值时出错

时间:2012-10-18 10:04:32

标签: android android-intent

使用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();
       }

    }

这是我在我的代码中找到的错误

enter image description here

4 个答案:

答案 0 :(得分:1)

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

private Activity activity;

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

然后使用activity.getIntent();

答案 1 :(得分:0)

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

activity.getIntent();

答案 2 :(得分: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

答案 3 :(得分:0)

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

因为持有此意图的类是内部类。