AsyncTask 和 setContentView
我有这个代码用于管理数据库中的某些国家/地区;
class checkCountryAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
setContentView(R.layout.main);
}
@Override
protected String doInBackground(String... aurl) {
MDPIActivity.this.runOnUiThread(new Runnable() {
public void run() {
CountryTable country = new CountryTable() ;
country.EnterCountry();
}
});
return null;
}
}
有了这个,我想设置内容视图,然后在后台,方法 onBackground 起作用,但我仍然需要等待内容视图,直到 onBackground 方法未完成。
谢谢。
I have this code for managing some countrys on my database;
class checkCountryAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
setContentView(R.layout.main);
}
@Override
protected String doInBackground(String... aurl) {
MDPIActivity.this.runOnUiThread(new Runnable() {
public void run() {
CountryTable country = new CountryTable() ;
country.EnterCountry();
}
});
return null;
}
}
With this, I would like to set the content View and then in background, that the method onBackground works, but I have still to wait for the content view until the onBackground method is not finished.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为有任何理由将
setContentView()
放在onPreExecute
方法中,它应该放在onCreate
方法中以避免任何类型当您尝试查找视图时,会发生NullPointerException
,对于您的AsyncTask
,您应该只使用在方法doInBackground()
i don't see any reason for putting the
setContentView()
in theonPreExecute
method, it should be in theonCreate
method to avoid any kind ofNullPointerException
when you will try to find your views, and for yourAsyncTask
, you should just use theonPostExecute()
which is executed after the methoddoInBackground()
请尝试一下这个,想想它的工作原理。`class checkCountryAsync extends AsyncTask {
}
`
Please try this one, think its work.`class checkCountryAsync extends AsyncTask {
}
`
您应该尝试publishProgress() 和onProgressUpdate() 方法。
You should try publishProgress() and onProgressUpdate() methods.