Adr*_*osz 4 c# android xamarin.android xamarin
当我按下任何按钮时,我得到:
System.NullReferenceException:未将对象引用设置为对象的实例.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
}
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>
Run Code Online (Sandbox Code Playgroud)
为什么?这段代码出了什么问题?这段代码很简陋,但我无法解决问题.
您的代码没有任何问题.
在运行时,Android无法找到您的imageProfile视图,因此FindViewById<ImageView>(Resource.Id.imageProfile)返回null.
尝试删除Resource.designer.cs和重建的内容(不是文件).
| 归档时间: |
|
| 查看次数: |
7449 次 |
| 最近记录: |