【问题标题】:How to pass Location type ArrayList to MapsActivity from MainActivity in android?如何将位置类型 ArrayList 从 Android 中的 MainActivity 传递给 MapsActivity?
【发布时间】:2017-09-10 18:35:38
【问题描述】:

我要做的是将位置列表传递给地图活动并在这些位置上放置标记。我试过在 MainActivity 中使用

public class MainActivity extends AppCompatActivity implements Parcelable {

ArrayList<Location> locs=new ArrayList<>();
...
locs.add(location);
...
Intent in = new Intent(context,MapsActivity.class);
in.putExtra("setlocations",locs);
startActivity(in);
...
 @Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(locs);

}
}

然后在MapsActivity的onCreate()中

 Bundle extras = getIntent().getExtras();
    if(extras != null){
        locs=(ArrayList<Location>)getIntent().getParcelableExtra("setlocations");
        addMarkeratLocation();
    }

addMarkeratLocation() 方法使用locs 列表使用 for 循环添加标记

public void addMarkeratLocation(){
    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.mipmap.dott);
    LatLng addpoint=new LatLng(0.0,0.0);
    for(int i=0;i<locs.size();i++){
        addpoint = new LatLng(locs.get(i).getLatitude(), locs.get(i).getLongitude());
        mMap.addMarker(new MarkerOptions().position(addpoint).icon(icon));
     }
    mMap.moveCamera(CameraUpdateFactory.newLatLng(addpoint));
}

当 Intent 被触发时,我的应用程序崩溃了。而logcat显示 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference 为什么显示null?这是我第一次使用 Parcelable 接口。我有什么遗漏的吗?任何意见将不胜感激,谢谢。

【问题讨论】:

    标签: android android-intent arraylist location


    【解决方案1】:
    `Location` class is not a Parcalable class.
    

    Intent.putExtra() 函数只接受原始和 Parcelable 数据类型。所以你不能通过意图传递List&lt;Location&gt;。相反,您可以使用 Gson 库来序列化您的位置数组列表并将其作为 Json 字符串传递给 MapActivity 并在 MapActivity 中将 Json 字符串反序列化到位置数组。

    【讨论】:

    • 你能提供任何例子吗?
    • 当然我会详细回答
    【解决方案2】:

    第 1 步:首先使用 Gradle 脚本导入 Gson 库

    在您的 Gradle 依赖项中添加这一行

    dependencies {
        compile 'com.google.code.gson:gson:2.7'
    }
    

    第二步:将 List 转换为 JSON 字符串,并将 JSON 字符串从 MainActivity 传递给 MapsActivity

            Location location1 = new Location("");
            location1.setLatitude(12.124);
            location1.setLongitude(77.124);
            Location location2 = new Location("");
            location2.setLatitude(12.765);
            location2.setLatitude(77.8965);
    
            List<Location> locations = new ArrayList<Location>();
            locations.add(location1);
            locations.add(location2);
    
            Gson gson = new Gson();
            String jsonString = gson.toJson(locations);
            Intent intent = new Intent(MainActivity.this,MapsActivity.class);
            intent.putExtra("KEY_LOCATIONS",jsonString);
            startActivity(intent);
    

    第 3 步:从 Bundle 中获取 JSON 字符串并将其转换为 List

            Bundle bundle = getIntent().getExtras();
            String jsonString = bundle.getString("KEY_LOCATIONS");
    
            Gson gson = new Gson();
            Type listOfLocationType = new TypeToken<List<Location>>() {}.getType();
            List<Location> locations = gson.fromJson(jsonString,listOfLocationType );
    

    希望对你有帮助!!

    【讨论】:

    • Type 包有多个导入选项。我应该导入哪个?
    • 导入 java.lang.reflect.Type;
    • 应用程序崩溃,在添加标记的行出现错误java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference,这就是我添加标记mMap.addMarker(new MarkerOptions().position(addpoint).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));的方式
    • 变量 location1 和 location2 不完整,这可能是问题,但我不确定。尝试传递完整的位置变量。但是上面的方法肯定会让你将 List 传递给其他 Activity
    • 我在onLocationChanged 方法中将位置添加到列表中
    【解决方案3】:

    ArrayList locs=new ArrayList(); Location 类应该用 Parcelable 实现,你已经实现了 Parcelable,但它是用于应该用于 Location 类的活动。

    请参阅以下链接以获取与您的查询相关的更多信息。

    Passing arraylist of custom object to another activity

    【讨论】:

    • 那个 Location 类是 android 的内置类......我怎样才能用 Parcelable 实现它??
    • 您需要创建自己的类,并且该类应该具有该位置类示例:- 创建另一个类 MyLocationClass ,因此数组将是 ArrayList 并且 MyLocationClass 应该使用 Parcelable 实现。
    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多