我有一個startActivity,它顯示一個警告對話框,直到我的異步任務完成了我的下載和解析xml。然後我去下一個startActivity。問題在於我爲startActivity的線程等待,屏幕上沒有顯示任何內容。如果我註釋掉命令startActivity,我會看到所有的東西。爲什麼是這樣?有人可以幫忙嗎?startActivity隱藏當前佈局和對話框
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false);
ad.setMessage("Loading Events");
ad.show();
if (!isNetworkAvailable()){
ad = new AlertDialog.Builder(this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage("Not Connected Exiting");
ad.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
ad.show();
}
downloadXML();
events=parseXML();
((CATApplication)this.getApplication()).setEvents(events);
try{
Thread.sleep(10000);
Intent intent = new Intent(this,EventsListActivity.class);
startActivity(intent);
}catch(Exception e){}
}
//check for network connection
private boolean isNetworkAvailable(){
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo=connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo.isConnectedOrConnecting();
}
public void onPause(){
File xmlFile = new File("deletefileonexit");
xmlFile.delete();
finish();
super.onPause();
}
private void downloadXML() {
String url = "locationofxmlonweb";
new DownloadFileAsync().execute(url);
}
public Events parseXML(){
Events newEvents=new Events();
try{
while(!(new File("locationofxml").exists())){}
InputStream in=new FileInputStream("locationofxml");
newEvents=new ParseEventsXML().parse(in);
}
catch (Exception e){}
return newEvents;
}
}
刪除:Thread.sleep(10000);' – Blundell