我有两个活动。第一个生成整数数组列表数据,我想在第二个活动中将数据用作 int[] 数据。我的第二个活动的代码如下:
public class maptask extends MapActivity {
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Intent intent= new Intent();
List<Integer> x=intent.getIntegerArrayListExtra("lat");
List<Integer> y=intent.getIntegerArrayListExtra("lon");
//receive the data from the first activity
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.ic_action_search);
mapoverlay itemizedoverlay = new mapoverlay(drawable, this);
int a=0, b=0;
for(a=0; a<x.size();a++)
//here, the logcat shows there must be something wrong.
for(b=0; b<y.size(); b++)
{
if(a==b)
{
GeoPoint Point =new GeoPoint(x.get(a),y.get(b));
OverlayItem overlayitem = new OverlayItem(Point, "Hola, Mundo!", "I'm in Mexico City!");
mapOverlays.add(itemizedoverlay);
itemizedoverlay.addOverlay(overlayitem);
}
}
}
}
为了找到问题所在,一开始尝试在代码开头定义两个2的int[]而不是arrarylist,运行成功。所以问题一定存在于数据类型传输或意图数据传输中。
下面是第一个活动:
公共类 MainActivity 扩展 Activity {
public List<String> position = new ArrayList<String>();
public List<Long> time=new ArrayList<Long>();
public List<Integer> lat= new ArrayList<Integer>();
public List<Integer> lon= new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button mapbutton=(Button) findViewById(R.id.button1);
mapbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent =new Intent();
intent.setClass(MainActivity.this, maptask.class);
intent.putIntegerArrayListExtra("lat", (ArrayList<Integer>) lat);
intent.putIntegerArrayListExtra("lon", (ArrayList<Integer>) lon);
intent.putStringArrayListExtra("data", (ArrayList<String>) position);
startActivity(intent);
}
});
final Button listbutton=(Button) findViewById(R.id.button2);
listbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent =new Intent();
intent.setClass(MainActivity.this, list.class);
intent.putStringArrayListExtra("data", (ArrayList<String>) position);
//String yes="my name";
//intent.putExtra("yes", yes);
startActivity(intent);
}
});
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private void additem(Location location){
String latLongString = "Lat:" + location.getLatitude() + "\nLong:" + location.getLongitude();
position.add(latLongString);
long t=location.getTime();
time.add(t);
}
private void addgeo(Location location){
int x=(int)location.getLatitude()*1000000;
int y=(int)location.getLongitude()*1000000;
lat.add(x);
lon.add(y);
}
//I use the addgeo to put the data into arraylist.
private LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
additem(location);
addgeo(location);
};
// updateWithNewLocation(location);
// addlayout(location);
public void onProviderDisabled(String provider){
//updateWithNewLocation(null);
// addlayout(null);
}
public void onProviderEnabled(String provider) {
//updateWithNewLocation(null);
// addlayout(null);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};