AsyncTask 结果与常规函数不同
我有一个接收滑动手势的视图。 这应该触发主活动的创建和 setContentView(layout) 。 (视图位于主活动的布局内)
当我尝试在异步任务中执行此操作时,它会遗漏布局的所有图像吗? 好像还没有结束。 可能有些事情我还没有完全理解。
这是来自 view.java 的部分代码,
Main.mHandler.post(new Runnable()
{
public void run()
{
new Main.GetNavigationLayout().execute(url);
}
});
url 是我用来创建新布局的 .xml 文件的位置。
GetNavigationLayout 是 AsyncTask
代码,它是 Main.java 中的 AsyncTask :
public static class GetNavigationLayout extends AsyncTask<String, Void, CustomLayout> {
boolean isDone = false;
@Override
protected EvadoLayout doInBackground(String... url)
{
CustomLayout layout = new CustomLayout(Main);
Looper.prepare();
try
{
String newpage = url[0];
Document doc = XmlImporter.GetNewPlayer(newpage);
if (doc == null)
{
layout = null;
}
layout.LoadXml(doc); // cause drawing of objects etc.
}
catch (Exception e)
{
e.printStackTrace();
}
isDone = true;
//Looper.loop(); // causes it to never return...
return layout;
}
@Override
protected void onPostExecute(CustomLayout layout)
{
if (isDone)
Main.setContentView(layout);
}
}
现在它显示除图像之外的所有内容,而如果我在没有 AsyncTask 的情况下运行它,它将显示布局中的所有内容。 我缺少什么?
I have a View that recieves swipe gestures.
this should trigger the creation and setContentView(layout) for the main activity.
(the View is within a Layout on the Main activity)
When i try to do this within an asynctask it leaves out all the images for the layout?
as if it hasn't finished yet .
There is prolly something i'm not fully understanding .
this the partial code from the view.java
Main.mHandler.post(new Runnable()
{
public void run()
{
new Main.GetNavigationLayout().execute(url);
}
});
the url is a location of an .xml file I use to create a new layout.
GetNavigationLayout is the AsyncTask
code that is the AsyncTask in Main.java :
public static class GetNavigationLayout extends AsyncTask<String, Void, CustomLayout> {
boolean isDone = false;
@Override
protected EvadoLayout doInBackground(String... url)
{
CustomLayout layout = new CustomLayout(Main);
Looper.prepare();
try
{
String newpage = url[0];
Document doc = XmlImporter.GetNewPlayer(newpage);
if (doc == null)
{
layout = null;
}
layout.LoadXml(doc); // cause drawing of objects etc.
}
catch (Exception e)
{
e.printStackTrace();
}
isDone = true;
//Looper.loop(); // causes it to never return...
return layout;
}
@Override
protected void onPostExecute(CustomLayout layout)
{
if (isDone)
Main.setContentView(layout);
}
}
now this shows everythig besides images , whereas if i run this without AsyncTask it displays everything in the layout.
what am i missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你是在这里画图的吧?我认为这就是问题所在。尝试从 AsyncTask 的
doInBackground()
进行绘制是错误的,因为它不是 UI 线程。您应该在 UI 线程中运行的onPostExecute()
中进行绘图I reckon you are drawing the images here? I think this is the problem. Trying to draw from the AsyncTask's
doInBackground()
is wrong, since it is not the UI thread. You should do the drawing inonPostExecute()
which runs in the UI thread您可以在 onProgressUpdate( )。使用此方法将允许您留在 doInBackground 内,并在获得布局并继续在 doInBackground 内时向 UI 发布更新。使用 publishProgress() 发送从 doInBackground 到 UI 线程的信息。
You can post to the UI in the onProgressUpdate(). Using this method will allow you to stay inside the doInBackground and post an update to the UI when you get a layout and continue inside the doInBackground. Use publishProgress() to send information from the doInBackground to the UI thread.