首页 > 解决方案 > FindViewById

问题描述

所以我在我的应用程序中添加了新按钮,但是当调用 FindViewById 时它返回 null 并且应用程序崩溃。在我的 .cs 文件中,我具有以下功能:

void fooUIButtonHandler()  
{  
    var fooUIButton = FindViewById(Resource.Id.fooUIButton);  
    fooUIButton.Click += delegate  
    {  
        if (!CheckOriginalImage()) 
        { 
            return; 
        } 

        Intent intent = new Intent(this, typeof(fooImageDemoActivity));  
        intent.PutExtra(fooImageDemoActivity.EXTRAS_ARG_IMAGE_FILE_URI,   
            originalImageUri.ToString());  
        StartActivityForResult(intent, REQUEST_SB_FOO_UI);  
    };  
} 

我的 .axml 文件如下所示:

<Button  
    android:id="@+id/fooUIButton"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="#0b8442"  
    style="@style/Widget.AppCompat.Button.Borderless"  
    android:text="foo"/>

我在 OnCreate 中设置内容视图:

SetContentView(Resource.Layout.FooDocumentCreation);

然后调用:

fooUIButtonHandler()

但以下行抛出 Null 异常:

var fooUIButton = FindViewById(Resource.Id.fooUIButton);

任何想法如何解决它?

标签: xamarin.android

解决方案


推荐阅读