Xamarin System.InvalidCastException:无法将“Android.Widget.EditText”类型的实例转换为“Android.Widget.Button”类型

时间:2016-11-28 05:48:01

标签: c# android xamarin

直到我添加

才出现此错误
private void ExList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        ExList.RemoveViewAt(e.Position);
        Openers.Remove(e.Position.ToString());
        adapter.NotifyDataSetChanged();
    }

完整代码只从Itext textview获取输入文本,并将其添加到Openers,然后转到ExList Listview。适配器很简单。 Itext.text =“”;用调试器突出显示,所以我假设它停在那一行。 这是代码。

public class MainActivity : Activity
    {

        public List<string> Openers = new List<string>();

        Button AButton;
        TextView Itext;
        ListView ExList;
        Button SettingsB;
        ArrayAdapter adapter;
        public OpenersView adap;//Custom adapter class


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);



            SettingsB = FindViewById<Button>(Resource.Id.SettingsB);
            AButton = FindViewById<Button>(Resource.Id.AddB);
            Itext = FindViewById<TextView>(Resource.Id.Itext);
            ExList = FindViewById<ListView>(Resource.Id.ExList);

            //adap = new OpenersView(this, Openers); //Custom adapter
            adapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1, Openers);//Simple adapter
            ExList.Adapter = adapter;


            AButton.Click += (object sender, EventArgs e) =>
            {

                if (Itext.Text.Length > 8) { Openers.Add(Itext.Text); }
                adap.NotifyDataSetChanged();
                Itext.Text = "";
                //I declare the listview here so it will update every click of the button.
            };

            ExList.ItemClick += ExList_ItemClick;



        }

        private void ExList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            ExList.RemoveViewAt(e.Position);
            Openers.Remove(e.Position.ToString());
            adapter.NotifyDataSetChanged();
        }


    }

}

1 个答案:

答案 0 :(得分:0)

在xml代码中检查您的Button。 确保Resource.Id.SettingsB和Resource.Id.AddB是按钮。

如果其editText像这样改变

        SettingsB = FindViewById< Edittext >(Resource.Id.SettingsB);   //check xml
        AButton = FindViewById< Edittext >(Resource.Id.AddB);         //check xml