c# –Xamarin.Android.你调用的对象是空的.单击按钮

c# –Xamarin.Android.你调用的对象是空的.单击按钮,第1张

概述当我按下任何按钮时,我得到:System.NullReferenceException:Objectreferencenotsettoaninstanceofanobject.MainActivity.csnamespaceXamarin.Android.Pluralsight{[Activity(Label="Xamarin.Android.Pluralsight",MainLauncher=true,Icon=&quo

当我按下任何按钮时,我得到:

System.NullReferenceException: Object reference not set to an instance of an object.

MainActivity.cs

namespace Xamarin.AndroID.Pluralsight{    [Activity(Label = "Xamarin.AndroID.Pluralsight", MainLauncher = true, Icon = "@drawable/icon")]    public class MainActivity : Activity    {        private button _buttonPrevIoUs;        private button _buttonNext;        private ImageVIEw _imageProfile;    protected overrIDe voID OnCreate(Bundle bundle)    {        base.OnCreate(bundle);        SetContentVIEw(Resource.Layout.Main);        _buttonPrevIoUs = FindVIEwByID<button>(Resource.ID.buttonPrevIoUs);        _buttonNext = FindVIEwByID<button>(Resource.ID.buttonNext);        _imageProfile = FindVIEwByID<ImageVIEw>(Resource.ID.imageProfile);        _buttonNext.Click += buttonNext_Click;        _buttonPrevIoUs.Click += buttonPrevIoUs_Click;    }    private voID buttonPrevIoUs_Click(object sender, EventArgs e)    {        _imageProfile.SetimageResource(Resource.Drawable.image1);    }    private voID buttonNext_Click(object sender, EventArgs e)    {        _imageProfile.SetimageResource(Resource.Drawable.image2);    }}

}

Main.axml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent">  <button      androID:ID="@+ID/buttonPrevIoUs"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:text="PrevIoUs"      androID:layout_alignParentBottom="true"      androID:layout_alignParentleft="true" />  <button      androID:ID="@+ID/buttonNext"      androID:layout_wIDth="wrap_content"      androID:layout_height="wrap_content"      androID:text="Next"      androID:layout_alignParentBottom="true"      androID:layout_alignParentRight="true" />  <ImageVIEw      androID:ID="@+ID/imageProfile"      androID:layout_wIDth="fill_parent"      androID:layout_height="wrap_content"      androID:src="@drawable/image1" /></relativeLayout>

为什么?这段代码出了什么问题?这段代码很简陋,但我无法解决问题.

解决方法:

您的代码没有任何问题.

在运行时,AndroID无法找到您的imageProfile视图,soFindVIEwByID< ImageVIEw>(Resource.ID.imageProfile)返回null.

尝试删除Resource.designer.cs的内容(而不是文件)并重建.

总结

以上是内存溢出为你收集整理的c# – Xamarin.Android.你调用的对象是空的.单击按钮全部内容,希望文章能够帮你解决c# – Xamarin.Android.你调用的对象是空的.单击按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1108417.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存