我有一個讓我瘋狂的小問題。 我有一個類(不活動),它擴展了Java類「線程」。在這個cass中,我將一些PostJson變量下載到Run()方法中。在課堂和活動之間傳遞陣列
在這個Run()方法中,我調用Object的構造函數,然後檢查剛下載的Json變量,以便創建一個新的Object。 所有這一切都爲Cicle中的每個JsonPost集合創建一個新對象。
public class GetChargePoint extends Thread{
JSONObject obj;
JSONArray jArray;
InputStream inputStream = null;
Handler handler;
private ConnectionSource connectionSource;
public ArrayList<ChargePoint_db> chargePointList = null;
public GetChargePoint(Handler handler) {
super();
this.handler = handler;
try {
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
...
for (int i = 0; i < jArray.length(); i++) {
try {
JSONObject oneObject = jArray.getJSONObject(i);
String name = oneObject.getString("Name");
String address = oneObject.getString("Address");
String description = oneObject.getString("Description");
String ownerTelNumber = oneObject
.getString("OwnerTelNumber");
String serialNumber = oneObject.getString("SerialNumber");
String dateModified = oneObject.getString("DateModified");
String id = oneObject.getString("Id");
int pointModelId = oneObject.getInt("PointModelId");
int maxNominalPowerKw = oneObject
.getInt("MaxNominalPowerkW");
boolean hasAcceleratedCharge = oneObject
.getBoolean("HasAcceleratedCharge");
boolean isInMaintenance = oneObject
.getBoolean("IsInMaintenance");
boolean active = oneObject.getBoolean("Active");
boolean isDeleted = oneObject.getBoolean("IsDeleted");
boolean hasFastCharge = oneObject
.getBoolean("HasFastCharge");
double latitude = oneObject.getDouble("Latitude");
double longitude = oneObject.getDouble("Longitude");
//calling the constructor.
Object object= new Object(id,
serialNumber, name, description, address,
maxNominalPowerKw, hasAcceleratedCharge,
hasFastCharge, ownerTelNumber, false, pointModelId,
active, isInMaintenance, 0, dateModified, latitude,
longitude);
chargePoint.save(repo);
chargePointList.add(object);
...
到目前爲止好。 但是你可以看到,我想填進一個ArrayList「chargePointList所有新對象創建。
的問題是,當到另一個活動我把這個類來獲取ArrayList中,這個返回null。 哪有我同意填補了類和其他活動之間的ArrayList?
謝謝:)
選中此項 - http://developer.android.com/guide/faq/framework.html#3 – Shaiful
該處理程序保留對創建它的上下文的引用,這可能是您頭痛的原因。 – mach
可能的重複:http://stackoverflow.com/questions/12820758/android-intent-use-intent-to-pass-class-objects-from-activities –