java将<LatLng,String>的数组列表从主活动传递到另一个类
我有一个名为latLng的类,它创建一个latLng和string的对象, 在MainActivity中,我创建了这种类型的arraylist并将json打包到其中。 现在我想将它传递给另一个类,但我尝试的所有操作都不起作用,程序崩溃,或者在第二个活动中将我的数组填充为null。 这是我的密码:
拉丁。爪哇:
public class latLng {
LatLng latLng;
String id;
latLng( LatLng latLng,String id) {
this.latLng = latLng;
this.id = id;
}
public String getLatLngId() {
return id;
}
public LatLng getLatLng() {
return latLng;
}
}
主要活动:
public class MainActivity extends AppCompatActivity {
ArrayList<LatLng> locationList=new ArrayList<>();
ArrayList<latLng> idList=new ArrayList<>();
Bundle bundle;
String json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getJson();
bundle = new Bundle();
bundle.putParcelableArrayList("locationList", locationList);
// Get the coordinates
locationList = bundle.getParcelableArrayList("locationList");
}
public void getJson() {
json = null;
try {
InputStream is = getAssets().open("vehicles-location.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
JSONArray jsonArray = new JSONArray(json);
for(int i=0;i<jsonArray.length();i++) {
JSONObject obj =jsonArray.getJSONObject(i);
String id=obj.getString("id");
JSONObject location = obj.getJSONObject("location");
double x = location.getDouble("lat");
double y = location.getDouble("lng");
LatLng m = new LatLng(x, y);
locationList.add(m);
latLng l=new latLng(m,id);
idList.add(l);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void MapOfVehicles(View view) {
Intent intent = new Intent(MainActivity.this, MapOfVehicles.class);
intent.putExtras(bundle);
startActivity(intent);
}
public void DrawPolygon(View view) {
Intent intent = new Intent(MainActivity.this, DrawPolygon.class);
intent.putExtra("bundle",bundle);
startActivity(intent);
}
}
画多边形。爪哇:
public class DrawPolygon extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap gMap;
Button btndraw, btndelete,btnlist;
Polygon polygon = null;
List<LatLng> latLngList = new ArrayList<>();
List<Marker> markerList = new ArrayList<>();
ArrayList<latLng> latLngIdlist = new ArrayList<>();
ArrayList<String> idlist;
Boolean created;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw_polygon);
btndraw = findViewById(R.id.btndraw );
btndelete = findViewById(R.id.btndelete );
btnlist=findViewById(R.id.btnlist);
created=false;
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
idlist =new ArrayList<>();
latLngIdlist =(ArrayList<latLng>) getIntent().getSerializableExtra("bundle");
错误:
java.lang.ClassCastException: 安卓.os.Bundle cannot be cast to java.io.Serializable
at 安卓.os.BaseBundle.getSerializable(BaseBundle.java:1233)
at 安卓.os.Bundle.getSerializable(Bundle.java:1034)
at 安卓.content.Intent.getSerializableExtra(Intent.java:7516)
at com.example.u1tema5安卓googlemaps.DrawPolygon.onCreate(DrawPolygon.java:70)
at 安卓.app.Activity.performCreate(Activity.java:7136)
at 安卓.app.Activity.performCreate(Activity.java:7127)
at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at 安卓.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at 安卓.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at 安卓.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at 安卓.os.Handler.dispatchMessage(Handler.java:106)
at 安卓.os.Looper.loop(Looper.java:193)
at 安卓.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:858)
有人能帮我吗
# 1 楼答案
按照下面的代码更改latLng类,并在主活动中使用Intent向您发送ArrayList。 我没有测试过这个,但我觉得效果不错