0
我想提出一個imageview的屏幕上,但。它給了「對象引用null」異常。我認爲我有一個邏輯錯誤。請幫我找到它。如何把ImageView的在移動應用[XAMARIN,ANDROID]
這裏是我的代碼:
我的佈局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);
}