xamarin.android - 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);
任何想法如何解决它?
解决方案
推荐阅读
- html - 向下树结构中的多下拉
- javascript - 如何在 html/css 中垂直堆积移动/平板电脑视图中的项目?
- react-native - 如何将这两个函数组合成一个带有两个参数的函数?
- c++ - 如何将图标添加到控制台应用程序
- mongodb - 根据另一个嵌入文档的搜索条件获取嵌入文档
- corda - 如何在cordapp中提及H2数据库表的主键
- mongoose - Mongoose 使用包含文档中至少一个数组元素的数组查询文档。
- python - 如何让scrapy爬虫在处理多级网页爬取时正常工作
- pytorch - Google Colab:从本地上传 Pytorch 模型后“无法连接到运行时”
- node.js - 为什么在节点 js 上使用 mongodb 时 $pull 不起作用