android-Monodroid选项卡布局示例项目似乎不起作用
作者:互联网
我正在Xamarin网站上显示的Tab Layout example方面苦苦挣扎.我已经创建了状态列表可绘制的xml文件,并按照此处的描述将图标复制到了我的可绘制的目录中,但是在使用OnCreate方法时遇到了麻烦.
他们列出的OnCreate方法显然已损坏,因为它缺少TabHost实例化.但是,即使通过调用来解决此问题
var TabHost = new TabHost(this);
我仍然收到null引用异常.这是OnCreate的完整源代码,直到抛出该行:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var TabHost = new TabHost(this);
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent(this, typeof(StopWatchActivity));
intent.AddFlags(ActivityFlags.NewTask);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = TabHost.NewTabSpec("Stoppuhr");
spec.SetIndicator("Artists", Resources.GetDrawable(Resource.Drawable.ic_tab_artists_grey));
spec.SetContent(intent);
//Crashes with a null reference exception
TabHost.AddTab(spec);
...
}
为什么TabHost.AttTab使用空引用异常使我的应用程序崩溃?
作为替代方案,如果您可以在其中下载可以正常工作的示例项目,以显示Monodroid在使用中的选项卡布局,我很乐意将其用作参考.
解决方法:
Xamarin的GitHub上的MonoDroid示例中有一个在APIDemo上工作的示例:https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Tutorials/TabLayoutTutorial.cs
您的活动是否属于TabActivity的子类? (
var TabHost = new TabHost(this);
根本不需要).
我希望这有帮助,
克里斯·NTR
标签:xamarin-android,android 来源: https://codeday.me/bug/20191202/2085004.html