0

我正在尝试将我自己的类的对象作为活动结果返回,但我得到的只是空指针……我阅读了一些教程,但没有足够的关于 Own 类结果的信息。

代码:

GPS客户端活动:

public class GPSClientActivity extends Activity {
private MapView view;
private MapDrawer mainDrawer;
private Intent intent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);
        view = (MapView) findViewById(R.id.mapView);
        view.setTileSource(TileSourceFactory.MAPNIK);
        view.setBuiltInZoomControls(true);
        MapController mapController = view.getController();
        mapController.setZoom(10);
        GeoPoint point2 = new GeoPoint(51496994, -134733);
        mapController.setCenter(point2);
        mainDrawer=new MapDrawer(view);
        final Button button = (Button) findViewById(R.id.addButton);
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                intent=new Intent(GPSClientActivity.this,WaypointSetupActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent,1);
                return false;
            }
        }); 

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK)
        {
            MapPoint point=(MapPoint) intent.getSerializableExtra("result");
             GeoPoint point2 = new GeoPoint(51496994, -134254);
             point.setGeo(point2);
             mainDrawer.addGeopointToMap(point.getName(), point.getDesc(), point.getPos());
        }
    }



}

WaypointSetupActivity:

public class WaypointSetupActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waypoint_setup);
        final Button button = (Button) findViewById(R.id.butConfirm);
        final EditText nameField=(EditText)findViewById(R.id.editText1);
        final EditText descField=(EditText)findViewById(R.id.editText2);
        button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                MapPoint point=new MapPoint("","", 1, 1, null);
                Intent result=new Intent();
                result.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                result.putExtra("result", point);
                setResult(WaypointSetupActivity.RESULT_OK,result);
                finish();
                return false;
            }
        }); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.waypoint_setup, menu);
        return true;
    }


}

地图点:

public class MapPoint implements Serializable
{
    private int priority;
    private int type;//person,building,vehicle,custom
    private String description;
    private String name;
    private GeoPoint position;

    public MapPoint(String nam,String desc,int typ,int prio,GeoPoint pos)
    {
        priority=prio;
        type=typ;
        description=desc;
        nam=name;
        position=pos;
    }
    public void setGeo(GeoPoint geo)
    {position=geo;}

    public int getPrio()
    {return priority;}

    public int getType()
    {return type;}

    public String getDesc()
    {return description;}

    public String getName()
    {return name;}

    public GeoPoint getPos()
    {return position;}
    public OverlayItem convertToOverlayItem(MapPoint point)
    {
        OverlayItem item=new OverlayItem(point.getName(),point.getDesc(),point.getPos());
        return item;
    }
}

地图抽屉:

public class MapDrawer 
{
    private ArrayList<OverlayItem> items;
    private ArrayList<MapPoint> mapPoints;
    private MapView currentMapview;
    public MapDrawer(MapView view)
    {
            GeoPoint geo=new GeoPoint(51496994, -134733);
            items=new ArrayList<OverlayItem>();
            mapPoints=new ArrayList<MapPoint>();
            currentMapview=view;
           // reconfigurePainter();- error
            //addGeopointToMap("meno","popis",geo);

    }

    public void addGeopointToMap(String name,String description,GeoPoint point)
    {
        MapPoint Mpoint=new MapPoint("meno","popis", 1, 1,point);
        mapPoints.add(Mpoint);
        items.add(Mpoint.convertToOverlayItem(Mpoint));
        reconfigurePainter();
    }

    public void reconfigurePainter()
    {
        DefaultResourceProxyImpl proxy=new DefaultResourceProxyImpl(currentMapview.getContext());
        ItemizedIconOverlay<OverlayItem> painter=new ItemizedIconOverlay<OverlayItem>(items, null, proxy);
        currentMapview.getOverlays().add(painter);
    }
}
4

1 回答 1

2

利用

MapPoint point=(MapPoint) data.getSerializableExtra("result");

代替

MapPoint point=(MapPoint) intent.getSerializableExtra("result");

因为intent是 null 你没有在intent内部初始化as :onActivityResultdata

 @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data) {
      // TODO Auto-generated method stub
     super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK)
        {
           intent=data; //<< assign data to intent
           MapPoint point=(MapPoint) intent.getSerializableExtra("result");
           //.....your code here..
        }
   }
于 2013-03-25T16:49:29.467 回答