我想在屏幕上放一个图像视图但是。它给出了“对象引用空”异常。我认为我有一个逻辑错误。请帮我找到它。
这是我的代码:
我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/changeImage"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/sampleviewer"
android:src="@drawable/sampleimage"
android:scaleType="fitCenter"/>
</LinearLayout>
在 OnCreate 函数中:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
//Create the user interface in code
var layout = new LinearLayout (this);
layout.Orientation = Orientation.Vertical;
var aLabel = new TextView (this);
aLabel.Text = "Hello, Xamarin.Android";
var aButton = new Button (this);
aButton.Text = "Say Hello";
aButton.Click += (sender, e) => {
aLabel.Text = "Hello from the button";
};
var button = new Button (this);
button.Text = "AAAAAA";
button.Click += delegate {
aLabel.Text = " PRESS THE BUTTON";
var imageView = FindViewById<ImageView> (Resource.Id.sampleviewer); // After this line imageView variable is still null
imageView.SetImageResource (Resource.Drawable.sampleimage);
};
layout.AddView (aLabel);
layout.AddView (aButton);
layout.AddView (button);
SetContentView (layout);
}