简体   繁体   English

Xamarin android按钮事件

[英]xamarin android button event

I've tried to test Button event but I'm getting NULL referenge error. 我尝试测试Button事件,但出现NULL引用错误。 There are three layouts :Main, simple_fragments and Stock 共有三种布局:Main,simple_fragments和Stock

There are one button (btSearch) and on edittext in the Stock -layout. 股票布局中有一个按钮(btSearch)和edittext。 Now when I try to create click-event: 现在,当我尝试创建点击事件时:

Button bt= FindViewById(Resources.Id.btSearch) 按钮bt = FindViewById(Resources.Id.btSearch)

bt.Click+=Bt_Click; bt.Click + = Bt_Click; <-- this cause error. <-这会导致错误。

If I put button to main layout and create button event everything works fine. 如果我将按钮放到主布局并创建按钮事件,则一切正常。

How create button events to each layout ? 如何为每个布局创建按钮事件?

Here is picture from MainActivity 这是MainActivity的图片

Lets hope that you can see read it. 希望您能看懂它。

Going by the comments, you are setting the content view to the wrong layout. 在评论旁,您正在将内容视图设置为错误的布局。

From your comment: 根据您的评论:

SetContentView(Resource.Layout.Main);

Yet, going from your post, the btSearch button is on the Stock layout. 但是,从您的帖子开始, btSearch按钮位于Stock布局上。 So you need to change that line to... 因此,您需要将该行更改为...

SetContentView(Resource.Layout.Stock);

FindViewById will return null if the view isn't found in the layout set in SetContentView . 如果在SetContentView设置的布局中SetContentView该视图,则FindViewById将返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM