【发布时间】:2019-01-04 10:25:25
【问题描述】:
我有问题。
这是我使用的简化代码:
public void LoadOrderPage()
{
Android.Support.V4.View.ViewPager SummaryWalletSwitcher =
FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.SummaryWalletSwitcher);
List<View> viewlist = new List<View>();
viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentSummary, null, false));
viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentWallet, null, false));
SummaryWalletAdapter ViewSwitchAdapter = new SummaryWalletAdapter(viewlist);
SummaryWalletSwitcher.Adapter = ViewSwitchAdapter;
LoadAgentInfo(null, null);
Timer AgentInfo_Timer = new Timer();
AgentInfo_Timer.Interval = 1000;
AgentInfo_Timer.Elapsed += LoadAgentInfo;
AgentInfo_Timer.Enabled = true;
}
public void LoadAgentInfo(object sender, ElapsedEventArgs e)
{
TextView TextView1 = FindViewById<TextView>(Resource.Id.txtPortfolioValue);
TextView1.Text = "This is TextView 1";
}
TextView 在Resource.Layout.AgentSummary 内。
计时器每秒运行良好!
但是当我调用LoadAgentInfo(null, null); 时,它说该函数内的 TextView 是一个空引用。原因是我使用 ViewPager 在一个页面中使用了 2 个布局。
我已经尝试过像这样从 id 的来源扩展布局:
var InflatedAgentSummary = LayoutInflater.Inflate(Resource.Layout.AgentSummary, null);
TextView TextView1 =
InflatedAgentSummary.FindViewById<TextView>(Resource.Id.txtPortfolioValue);
但是 TextView 永远不会改变!
我做错了什么?
【问题讨论】:
-
确保 ID 为
txtPortfolioValue的 textview 是该活动的布局集的一部分。该活动的代码在OnCreate方法上的外观如何? -
好吧,TextView 所在的布局在 Android.Support.V4.View.ViewPager 内部
-
我会将代码编辑为我拥有的更相似的版本
-
我已经编辑了问题和代码
-
如果 TextView 在另一个视图中,在本例中为
ViewPager,那么您将无法从活动中找到它。这就是您拥有空引用的原因。FindViewById仅在您使用SetContentLayout方法设置的布局文件中查找视图的 Id。在这里,您可能需要找到一种从该视图公开方法的方法。
标签: c# android xamarin methods elapsedtime