【问题标题】:How to transfer the int[] data from one activity to another activity by using intent如何使用意图将 int[] 数据从一个活动传输到另一个活动
【发布时间】:2012-11-19 19:23:46
【问题描述】:

我有两个活动。第一个生成整数数组列表数据,我想在第二个活动中将数据用作 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) {}
  };

【问题讨论】:

  • 所以你是用putIntegerArrayListExtra来传输数据的?
  • 是的,代码是这样的:intent.putIntegerArrayListExtra("lat", (ArrayList) lat); intent.putIntegerArrayListExtra("lon", (ArrayList) lon);
  • 发生哪一行NPE异常?
  • 这不是将整数数组转换为数组列表的正确方法。
  • 我在上面的代码中标记了第一个for循环行,我猜是这里的“x.size()”。

标签: android google-maps arraylist integer int


【解决方案1】:

它不起作用,因为在您的第二个活动中

 Intent intent= new Intent();

应该是

Intent intent = getIntent();

【讨论】:

  • 是的,你说得对。我怎么会犯这个愚蠢的错误。无论如何,非常感谢!
【解决方案2】:

如果您正在使用int[] lats,您应该使用:

intent.putExtra("lat", lats);

然后用getIntArrayExtra()阅读它:

int[] lats = intent.getIntArrayExtra("lat");

【讨论】:

  • 对我来说问题是在第一个活动中我必须使用整数数组列表来添加数据,那么如何将整数数组列表转换为整数数组?
  • List 还是ArrayList
  • @HeikiCyan @HeikiCyan int[]ArrayList&lt;Integer&gt; 是两种截然不同的数据类型...发布您的第一个 Activity 中的代码,以便我们了解您在使用什么以及如何使用它。
  • @HeikiCyan 将所有List 更改为ArrayList,因此您无需进行转换。
  • @HeikiCyan 查看我的回答,但最好将它们声明为ArrayList,这样您就不需要强制转换它们了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
相关资源
最近更新 更多