0

自分のクラスのオブジェクトをアクティビティの結果として返そうとしていますが、取得するのはnullpointerだけです...いくつかのチュートリアルを読みましたが、自分のクラスの結果に関する十分な情報がありませんでした。

コード:

GPSClientActivity:

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


}

MapPoint:

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

MapDrawer:

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");

intentnullであるため、intent内部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 に答える