【发布时间】:2020-01-06 10:10:24
【问题描述】:
我对应用程序开发还很陌生,但我有一个无法解决的问题。
我有一个启动画面,用于加载应用程序需要运行的各种内容(配置文件、来自 Internet 的 html),而后者给我带来了很大的问题。这是代码
Document doc = null;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
//Fix loading data, dunno why this happens
try {
doc = new getHtml().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Utils.saveData(Splash.this, doc);
Intent mainIntent = new Intent(Splash.this, PocketFrameNavigation.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
public static class getHtml extends AsyncTask<Void, Void, Document> {
protected Document doInBackground(Void... args) {
try {
return Jsoup.connect(DROP_DATA_URL).get();
} catch(Exception e) {
return null;
}
}
protected void onPostExecute(Document html){
doc = html;
}
}
给我一个问题的代码是 try 语句的内部。无论我把它放在哪里,它似乎都会冻结主线程。我做错了什么吗?提前感谢您的帮助。
此外,只要不涉及延迟后处理程序,getHTML 函数就会起作用。所以我认为这与此有关。
【问题讨论】:
标签: java android android-studio android-asynctask