1

画面にイメージビューを配置したいのですが。「オブジェクト参照null」例外が発生しました。論理的な誤りがあると思います。見つけるのを手伝ってください。

これが私のコードです:

私のレイアウト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);


   }
4

1 に答える 1