如何将Imageview放入移动应用程序[XAMARIN,ANDROID]

如何将Imageview放入移动应用程序[XAMARIN,ANDROID],android,android-imageview,xamarin,Android,Android Imageview,Xamarin,我想在屏幕上显示imageview,但是。它给出了“对象引用null”异常。我认为我有一个逻辑错误。请帮我找到它 这是我的密码: 我的布局xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" androi

我想在屏幕上显示imageview,但是。它给出了“对象引用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);


   }
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
//在代码中创建用户界面
var布局=新的线性布局(本);
布局。方向=方向。垂直;
var aLabel=新文本视图(此);
aLabel.Text=“你好,Xamarin.Android”;
var aButton=新按钮(此按钮);
aButton.Text=“打招呼”;
aButton.单击+=(发件人,e)=>{
aLabel.Text=“通过按钮打招呼”;
};  
var按钮=新按钮(此按钮);
button.Text=“AAAAAA”;
按钮。单击+=委派{
aLabel.Text=“按下按钮”;
var imageView=findviewbyd(Resource.Id.sampleviewer);//此行之后,imageView变量仍然为null
imageView.SetImageResource(Resource.Drawable.sampleimage);
};
layout.AddView(阿拉贝尔);
layout.AddView(阿布顿);
layout.AddView(按钮);
SetContentView(布局);
}

您在这里以两种不同的方式构建布局

  • 您有一个包含
    ImageView
    的XML布局,但您从未在代码中的任何位置引用该XML布局,因此Android从未加载它(因此您无法访问它)
  • 通过创建新的
    LinearLayout
    对象并向其添加其他视图,还可以在Java代码中创建布局。此布局没有
    ImageView
  • 在此处调用
    SetContentView()
    时:

    SetContentView (layout);
    
    您告诉Android使用您在代码中创建的布局。如果要使用XML布局,可以更改为:

    SetContentView(Resource.Layout.Main);
    
    (将
    Main
    替换为XML文件的实际名称。)

    否则,您需要在Java代码中添加
    ImageView
    。但是,您可能不想将XML布局与Java代码布局混为一谈

    还请注意,在调用
    FindViewById()
    之前,必须调用
    SetContentView()

    您可能还想阅读Xamarin的教程