从Android活动获取结果时出现nullPointerException

时间:2013-03-25 16:46:29

标签: android nullpointerexception android-activity

我试图将我自己的类的对象作为活动结果返回,但我得到的只是nullpointer ...我读了一些教程,但是关于Own类结果的信息不足。

代码:

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

1 个答案:

答案 0 :(得分:2)

使用

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

而不是

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

因为intent为空,您不会在intentonActivityResult初始化data作为:

 @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..
        }
   }