这种错误背后的原因是什么?

时间:2016-11-10 11:46:17

标签: c# android-layout xamarin.android

我在部署后运行应用程序时遇到以下错误。

"Android.Content.Res.Resources+NotFoundException: File res/drawable-mdpi-  v4/submenulistitem_background_top.xml from drawable resource ID #0x7f02008e"  

给出错误的代码如下:

txtListItem.SetBackgroundResource(Resource.Drawable.SubMenuListItem_Background_Top);  

我在可绘制文件夹中存在xml资源,资源条目也存在于resource.designer.cs文件中。该应用程序运行良好的最新Android设备,但在旧的Android设备之一给出此错误。旧设备有api v10。

当drawable在* .axml中给出为 - android:background =" @ drawable / something"对于v10设备,它查找并绘制UI,但是当以编程方式设置drawable时,则会发生此类错误,适配器的代码为:

using Android.App;
using Android.Views;
using Android.Widget;

namespace Greenlee.Ethernet.AndroidDevice
{
  public class SubMenuListViewAdapter : BaseAdapter<string>
  {
    string[] items;
    Activity context;

    public SubMenuListViewAdapter(Activity context, string[] items) : base()
    {
        this.context = context;
        this.items = items;
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override string this[int position]
    {
        get { return items[position]; }
    }

    public override int Count
    {
        get { return items.Length; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView; // re-use an existing view, if one is available
        TextView txtListItem = null;

        if (view == null) // otherwise create a new one
            view = context.LayoutInflater.Inflate(Resource.Layout.SubMenuListItemLayout, null);

        txtListItem = view.FindViewById<TextView>(Resource.Id.text1);

        if (position == 0)
        {
            txtListItem.SetBackgroundResource(Resource.Drawable.SubMenuListItem_Background_Top); // error line, I checked with setBackgroundDrawable() method too, but no luck.
        }
        else if (items.Length - position == 1)
        {
            txtListItem.SetBackgroundResource(Resource.Drawable.submenulistitembackgroundbottom);
        }
        else
        {
            txtListItem.SetBackgroundResource(Resource.Drawable.submenulistitembackground);
        }

        txtListItem.Text = items[position];
        return view;
    }
  }
}

0 个答案:

没有答案