android - Monodroid Tab Layout 示例项目似乎不起作用

标签 android xamarin.android

我正在为 Tab Layout example 苦苦挣扎显示在 Xamarin 的网站上。我已经创建了状态列表可绘制 xml 文件,并将图标复制到我的可绘制目录中,但我在使用 OnCreate 方法时遇到了问题。

他们列出的 OnCreate 方法显然已损坏,因为它缺少 TabHost 实例化。但即使通过调用

来解决这个问题

var TabHost = new TabHost(this);

我仍然得到空引用异常。这是 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

您的 Activity 是 TabActivity 的子类吗? (

var TabHost = new TabHost(this);

根本不需要)。

希望对你有帮助

克里斯NTR

关于android - Monodroid Tab Layout 示例项目似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456211/

相关文章:

javascript - 更改开放层 3 中 ol.geom.LineString 的颜色

android - scrollTo 始终有效,smoothScrollTo 仅有时有效

android - 逆向工程 Android Assets

c# - 子属性更新调用它的父属性 `OnPropertyChanged`

xamarin - 如何在 Xamarin Android 中使用 V8 JavaScript 引擎

java - 创建实现 Serialized/Parcelable 的 Firebase POJO 类

c# - Xamarin MotionEventActions.Move 永远不会在自定义 View onTouch 事件中被命中?

android - 未声明 android.support.design.widget.CoordinatorLayout

xamarin - 如何更改Android(Xamarin Forms)上按钮的文本对齐方式?

java - 在Appium(Android)中获取所有子笔记仅比父对象低一级