【发布时间】:2018-08-05 14:17:26
【问题描述】:
这是我的代码。我正在获取数据,但 name1 返回空值。我想在 textView 和 imageview 中以数据集的形式显示从 web 服务获取的数据。你能帮帮我吗?
当它执行name1.SetText(productName);行时
错误获取是
未处理的异常:
System.NullReferenceException:对象引用未设置为实例 的一个对象。发生了
下面是本地变量存储的内容。
名称值类型
detailOfproduct "瓶子是"字符串
fdetail "瓶子是"字符串
fimage "iVBORw0" 字符串
fname "瓶" 字符串
imageproduct "iVBORw0" 字符串
productName“瓶”字串
name1(空)
Android.Widget.TextView详细信息(空)
Android.Widget.TextView
WebReferencec.WebService add = new WebReferencec.WebService();
DataSet result = add.getData("bottle");
DataTable dt = result.Tables["product"];
//DataRow row = dt.NewRow();
fname = dt.Rows[0]["name"];
fdetail= dt.Rows[0]["detail"];
fimage = dt.Rows[0]["image"];
// dt.Rows.Add(row);
imageproduct = Convert.ToString(fimage);
productName = Convert.ToString(fname);
detailOfproduct = Convert.ToString(fdetail);
byte[] ba = Convert.FromBase64String(imageproduct);
using (var stream = new MemoryStream(ba))
{
ImageView image = FindViewById<ImageView>(Resource.Id.ivgallery);
TextView name1 = FindViewById<TextView>(Resource.Id.tvname);
TextView detail = FindViewById<TextView>(Resource.Id.tvdetail);
Bitmap bitmap = BitmapFactory.DecodeByteArray(ba, 0, ba.Length);
RunOnUiThread(() =>
{
image.SetImageBitmap(bitmap);
name1.SetTextKeepState( productName);
detail.SetTextKeepState( detailOfproduct);
}
);
}
【问题讨论】:
-
在此之前您是否致电
SetContentView?如果是这样,此布局是否包含您要查找的那些视图?你的代码在哪里调用? -
是的,但我确实解决了问题。我放了 var image 而不是 imageview 图像。而不是 textview 名称我把 var name... 细节相同
-
这不会改变任何事情。听起来重建可以解决资源未生成的问题...
-
it 它确实有所不同,因为 textview 是一个对象,其中 var 字符串。我确实尝试构建它并运行另一个项目和笔记本电脑!它没有用。但与 var 配合得很好