【问题标题】:Retrieve data from dataset webservice in Xamarin and set it to textview从 Xamarin 中的数据集 Web 服务中检索数据并将其设置为 textview
【发布时间】: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 配合得很好

标签: android xamarin dataset


【解决方案1】:

当我通过放置图像视图和文本视图来声明变量图像名称和详细信息时,变量不在字符串中。所以我不得不把 var infront 转换成字符串。

Var image = FindViewById(Resource.Id.ivgallery);

Var name1 = FindViewById(Resource.Id.tvname);

var detail = FindViewById(Resource.Id.tvdetail);

【讨论】:

    猜你喜欢
    • 2012-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多