Android 在主服务器上运行AsyncTask时出错
然而,创建Android 在主服务器上运行AsyncTask时出错,android,android-asynctask,Android,Android Asynctask,然而,创建AsyncTask的全部目的是为了摆脱使用该代码的需要 更新:我把image.setImageBitmap(img)放在执行后,它就工作了。谢谢大家 您无法从doInbackground()更新uidoInbackground。Ui应该在Ui线程上更新onPostExecute在ui线程上调用。doInbackground计算的结果是onPostExecute的一个参数。因此,在doInbackground中返回结果,并在onPostExecute中更新ui image.se
AsyncTask
的全部目的是为了摆脱使用该代码的需要
更新:我把image.setImageBitmap(img)放在执行后,它就工作了。谢谢大家 您无法从
doInbackground()
更新ui<在后台线程上调用code>doInbackground。Ui应该在Ui线程上更新<代码>onPostExecute在ui线程上调用。doInbackground
计算的结果是onPostExecute
的一个参数。因此,在doInbackground
中返回结果,并在onPostExecute
中更新ui
image.setImageBitmap(img);
本节下的检查主题包含上述链接中的4个步骤
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build();
StrictMode.setThreadPolicy(policy);
您应该在onPostExecute
中更新ui,或者使用runOnUiThread
但我建议您在onPostExecute
中更新ui
image.setImageBitmap(img);
无法从
doInbackground()
更新ui<在后台线程上调用code>doInbackground。Ui应该在Ui线程上更新<代码>onPostExecute在ui线程上调用。doInbackground
计算的结果是onPostExecute
的一个参数。因此,在doInbackground
中返回结果,并在onPostExecute
中更新ui
image.setImageBitmap(img);
本节下的检查主题包含上述链接中的4个步骤
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build();
StrictMode.setThreadPolicy(policy);
您应该在onPostExecute
中更新ui,或者使用runOnUiThread
但我建议您在onPostExecute
中更新ui
image.setImageBitmap(img);
我认为您正在
doInBackground()
方法中执行UI操作
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
// update ui
}
});
您应该在onPostExecute()
方法中执行此操作
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
// update ui
}
});
您应该只在
doInBackground()
方法中执行非UI操作。我认为您正在doInBackground()
方法中执行UI操作
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
// update ui
}
});
您应该在onPostExecute()
方法中执行此操作
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
// update ui
}
});
您应该只在
doInBackground()
方法中执行非UI操作。您不能在doInBackground()
中更新UI,您只需要在onPostExecute()
中更新UI。
所以使用下面的代码来更新图像
image.setImageBitmap(img);
公共类获取扩展异步任务
{
位图图像;
@凌驾
受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
URL=新URL(“http://c69282.r82.cf3.rackcdn.com/IMG_0755-Edit-4.jpg") ;
URLConnection=url.openConnection();
InputStream in=新的BufferedInputStream(connect.getInputStream());
img=BitmapFactory.decodeStream(in);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(位图结果)
{
设置图像位图(img);
}
}
在这里,您还提到了有关变量
图像的任何内容。您应该在onPostExecute()
中膨胀Imageview,然后添加设置图像位图的行。您不能在doInBackground()
中更新UI,您只需要在onPostExecute()
中更新UI。
所以使用下面的代码来更新图像
image.setImageBitmap(img);
公共类获取扩展异步任务
{
位图图像;
@凌驾
受保护的Void doInBackground(字符串…参数){
//TODO自动生成的方法存根
试一试{
URL=新URL(“http://c69282.r82.cf3.rackcdn.com/IMG_0755-Edit-4.jpg") ;
URLConnection=url.openConnection();
InputStream in=新的BufferedInputStream(connect.getInputStream());
img=BitmapFactory.decodeStream(in);
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(位图结果)
{
设置图像位图(img);
}
}
在这里,您还提到了有关变量图像的任何内容。您应该在onPostExecute()
中膨胀Imageview,然后添加设置图像位图的行。日志猫在崩溃时说什么?查看我的更新答案日志猫在崩溃时说什么?查看我的更新答案谢谢我会尝试。谢谢我会尝试。