我有两项活动。第一个生成整数arraylist数据,我想在第二个活动中将数据用作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) {}
};
答案 0 :(得分:1)
如果您正在使用int[] lats
,则应使用:
intent.putExtra("lat", lats);
并使用getIntArrayExtra()
阅读:
int[] lats = intent.getIntArrayExtra("lat");
答案 1 :(得分:1)
它不起作用,因为在你的第二个活动中
Intent intent= new Intent();
应该是
Intent intent = getIntent();