当我使用findinbackground方法从解析下载数据时,我的项目出了问题。所以看看我的代码(loadAllData method()和LoadDataAsyncTask)。我想在这个Activity中加载数据,我将在不同的活动中使用它但是当我在不同的活动中调用我的onjects时它是null。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
new LoadDataAsynceTask().execute();
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.container, new HomeFragment(), FRAGMENT_HOME_TAG);
transaction.commit();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
private class LoadDataAsynceTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void...params) {
loadAllData();
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(FindActivity.this,
"Stastions "+stations.size()+
"\nTimes "+stationTimes.size()+
"\nTaxis "+taxis.size()
,LENGTH_LONG).show(); // "Station 0 stationTime 0 Taxis 0"
}
@Override
protected void onProgressUpdate(Void...values) {
}
@Override
protected void onPreExecute() {
}
}
public void loadAllData() {
ParseQuery<ThoiGianBusStation> query = new ParseQuery<>("ThoiGian");
query.findInBackground(new FindCallback<ThoiGianBusStation>() {
@Override
public void done(List<ThoiGianBusStation> list, ParseException e) {
for (ThoiGianBusStation i : list) {
ThoiGianBusStation newTime = new ThoiGianBusStation();
newTime.setPid(i.getPid());
newTime.setPTime(i.getPTime());
newTime.setPTimeId(i.getPTimeId());
stationTimes.add(newTime);
}
Toast.makeText(FindActivity.this, "Times "+stationTimes.size() ,LENGTH_LONG).show();
// "statitonTimes 13"
}
});
ParseQuery<ChuyenXe> query1 = new ParseQuery<>("BusStation");
query1.findInBackground(new FindCallback<ChuyenXe>() {
@Override
public void done(List<ChuyenXe> list, ParseException e) {
for (ChuyenXe chuyenXe : list) {
ChuyenXe newXe = new ChuyenXe();
newXe.setPHangxe(chuyenXe.getPHangxe());
newXe.setPGotime(chuyenXe.getPGotime());
newXe.setPTransforment(chuyenXe.getPTransforment());
newXe.setPTram(chuyenXe.getPTram());
newXe.setPGia(chuyenXe.getPGia());
newXe.setPLoai(chuyenXe.getPLoai());
newXe.setPNoidi(chuyenXe.getPNoidi());
newXe.setPNoiden(chuyenXe.getPNoiden());
newXe.setPSDT(chuyenXe.getPSDT());
newXe.setPService(chuyenXe.getPService());
newXe.setPXeID(chuyenXe.getPXeID());
for (int i = 0; i < stationTimes.size(); i++) {
if (newXe.id == stationTimes.get(i).xe_id) {
newXe.addThoiGian(stationTimes.get(i).thoigian);
}
}
stations.add(newXe);
}
Toast.makeText(FindActivity.this, "Station " + stations.size(), LENGTH_LONG).show();
// "Station 4"
}
});
ParseQuery<Taxi> query2 = new ParseQuery<>("Taxi");
query2.findInBackground(new FindCallback<Taxi>() {
@Override
public void done(List<Taxi> list, ParseException e) {
for (Taxi taxi : list){
Taxi newTaxi = new Taxi();
newTaxi.setPname(taxi.getPname());
newTaxi.setPCost1(taxi.getPCost1());
newTaxi.setPCost2(taxi.getPCost2());
newTaxi.setPCost3(taxi.getPCost3());
newTaxi.setPkv(taxi.getPkv());
newTaxi.setPsdt(taxi.getPsdt());
newTaxi.setPWay1(taxi.getPWay1());
newTaxi.setPWay2(taxi.getPWay2());
taxis.add(newTaxi);
}
Toast.makeText(FindActivity.this, "Taxis "+taxis.size() ,LENGTH_LONG).show();
// "Taxis 3"
}
});
}
}
ChuyenXe班:
public class ChuyenXe extends ParseObject {
public int getPXeID(){
return getInt("xe_id");
}
public void setPXeID(int noidi) {
put("xe_id", noidi);
}
public String getPTransforment(){
return getString("transforment");
}
public void setPTransforment(String noidi) {
put("transforment", noidi);
}
public String getPTram(){
return getString("tram_dung_chan");
}
public void setPTram(String noidi) {
put("tram_dung_chan", noidi);
}
public String getPService(){
return getString("services");
}
public void setPService(String noidi) {
put("services", noidi);
}
public String getPSDT(){
return getString("sdt");
}
public void setPSDT(String noidi) {
put("sdt", noidi);
}
public String getPLoai(){
return getString("loaixe");
}
public void setPLoai(String noidi) {
put("loaixe", noidi);
}
public String getPHangxe(){
return getString("hangxe");
}
public void setPHangxe(String noidi) {
put("hangxe", noidi);
}
public String getPGotime(){
return getString("go_time");
}
public void setPGotime(String noidi) {
put("go_time", noidi);
}
public String getPGia(){
return getString("gia");
}
public void setPGia(String noidi) {
put("gia", noidi);
}
public String getPNoiden(){
return getString("bendi");
}
public void setPNoiden(String noidi) {
put("benden", noidi);
}
public String getPNoidi(){
return getString("bendi");
}
public void setPNoidi(String noidi) {
put("bendi", noidi);
}
ThoiGianBusStation类:
public class ThoiGianBusStation extends ParseObject{
public int getPTimeId(){
return getInt("time_id");
}
public void setPTimeId(int timeId) {
put("time_id", timeId);
}
public int getPid(){
return getInt("xe_id");
}
public void setPid(int name) {
put("xe_id", name);
}
public String getPTime(){
return getString("time");
}
public void setPTime(String time) {
put("time", time);
}
出租车类:
public class Taxi extends ParseObject{
public String getPname(){
return getString("name");
}
public void setPname (String noidi) {
put("name", noidi);
}
public String getPsdt(){
return getString("sdt");
}
public void setPsdt (String noidi) {
put("sdt", noidi);
}
public String getPkv(){
return getString("khuvuc");
}
public void setPkv(String noidi) {
put("khuvuc", noidi);
}
public int getPWay2(){
return getInt("doan2");
}
public void setPWay2(int noidi) {
put("doan2", noidi);
}
public int getPWay1(){
return getInt("doan1");
}
public void setPWay1(int noidi) {
put("doan1", noidi);
}
public int getPCost2(){
return getInt("cost2");
}
public void setPCost2(int noidi) {
put("cost2", noidi);
}
public int getPCost3(){
return getInt("cost3");
}
public void setPCost3(int noidi) {
put("cost3", noidi);
}
public int getPCost1(){
return getInt("cost1");
}
public void setPCost1(int noidi) {
put("cost1", noidi);
}
答案 0 :(得分:0)
问题是findInBackground()
,您应该使用find()
进行更改,因为您已经执行了asnc任务,&#34; findInBackground()&#34;创建新的后台线程,当你的asnc任务完成后,你可能无法获得结果。因为你的asnc任务不会等待Parse后台线程。您应该使用find()
进行更改。 Parse在后台任务中建议findInBackground()
。