我有一个 startActivity,它显示一个警报对话框,直到我在异步任务中完成对 xml 的下载和解析。然后我去下一个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;
}
}