gpt4 book ai didi

c# - Xamarin.安卓。你调用的对象是空的。点击按钮

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:01 24 4
gpt4 key购买 nike

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

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 View ,因此 FindViewById<ImageView>(Resource.Id.imageProfile)正在返回 null。

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

关于c# - Xamarin.安卓。你调用的对象是空的。点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com