Mono for android 线程 异步加载图片 并关闭进度条

破碎的脸 2016-01-06 02:46:56
Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views.

[Activity(Label = "阿伟", Icon = "@drawable/icon", Theme = "@android:style/Theme.Dialog")]
public class ModeActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ModeActivity);
StrictMode.SetThreadPolicy(new StrictMode.ThreadPolicy.Builder().DetectDiskReads().DetectDiskWrites().DetectNetwork().PenaltyLog().Build());
StrictMode.SetVmPolicy(new StrictMode.VmPolicy.Builder().DetectLeakedSqlLiteObjects().DetectLeakedClosableObjects().PenaltyLog().PenaltyDeath().Build());
Button btn = FindViewById<Button>(Resource.Id.bntCloseWindow1);
btn.Click += delegate { this.Finish(); };
var it = this.Intent;
string title = it.GetStringExtra("Title");
Toast.MakeText(this, title, ToastLength.Long).Show();
FindViewById<TextView>(Resource.Id.txtModeText).Text = title;
var probar = FindViewById<ProgressBar>(Resource.Id.progressBar1);
probar.Visibility = ViewStates.Visible;
new Thread(() =>
{
try
{
var img = FindViewById<ImageView>(Resource.Id.imageView1);
URL url = new URL("http://e.hiphotos.baidu.com/image/pic/item/d31b0ef41bd5ad6ea96731e683cb39dbb6fd3c1b.jpg");
img.SetImageBitmap(BitmapFactory.DecodeStream(url.OpenStream()));
}
catch { }
probar.Visibility = ViewStates.Gone;
}).Start();
}

能帮我纠正一下以上代码的错误吗?我想的是,在图片加载完成之后,关闭进度条。就这么简单。图片是放在网上的。
...全文
165 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孤独de猫 2016-01-06
  • 打赏
  • 举报
回复
我写android,直接用的eclipse adt 开发的。C# 只做windows的。
Justin-Liu 2016-01-06
  • 打赏
  • 举报
回复
debug下
破碎的脸 2016-01-06
  • 打赏
  • 举报
回复
new Thread(() => { bm = Tools.getImage("http://h.hiphotos.baidu.com/image/pic/item/8b82b9014a90f6038cefab923d12b31bb151ed8c.jpg"); Thread.Sleep(1000); iv = FindViewById<ImageView>(Resource.Id.imageView1); iv.Post(() => { iv.SetImageBitmap(bm); probar.Visibility = ViewStates.Gone; }); }).Start(); 我想把分给自己可以不?
破碎的脸 2016-01-06
  • 打赏
  • 举报
回复
好像真的没什么人懂啊?

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧