You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为ActionBar添加Tabs时出现对象未引用实例错误求助

解决ActionBar设置Tabs时NavigationMode报错的问题

我来帮你排查这个报错问题,大概率是主题配置错误或者代码调用顺序的问题,咱们一步步来解决:

1. 先检查应用主题(最常见原因)

如果你的App使用的是不带ActionBar的主题(比如Theme.AppCompat.Light.NoActionBar),那ActionBar对象会是null,设置它的NavigationMode自然会抛出空引用异常。

打开Resources/values/styles.xml(如果是适配Android 5.0+的项目,还要检查values-v21/styles.xml),确保你的主题是带ActionBar的:

<!-- 使用AppCompat主题的例子 -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- 自定义主题属性 -->
</style>

<!-- 或者使用原生Holo主题(适合较低版本Android) -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
</style>

2. 修正代码的调用顺序和冗余代码

你代码里重复调用了两次SetContentView(Resource.Layout.activity_main);,这会导致布局重复加载,而且要确保RequestWindowFeature在base.OnCreate之前执行,另外建议加个ActionBar非空判断,避免空引用:

protected override void OnCreate(Bundle bundle)
{
    try
    {
        // 必须在base.OnCreate之前调用RequestWindowFeature
        RequestWindowFeature(WindowFeatures.ActionBar);
        base.OnCreate(bundle);
        // 只保留一次SetContentView调用
        SetContentView(Resource.Layout.activity_main);

        // 先判断ActionBar是否存在,避免空引用报错
        if (ActionBar == null)
        {
            throw new Exception("当前主题不支持ActionBar,请检查主题配置");
        }

        ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
        Models.TableModel model = new Models.TableModel();
        List<Tables> tables = model.loadTables();
        _fragments = new Fragment[] { 
            new AllTablesFragment(tables), 
            new ReservedFragment(tables.Where(a=>a.is_reserved==true).ToList()), 
            new VacantFragment(tables.Where(a=>a.is_reserved==false).ToList()) 
        };
        AddTabToActionBar(Resource.String.all_tables, Resource.Drawable.tables);
        AddTabToActionBar(Resource.String.reserved_tables, Resource.Drawable.reserved);
        AddTabToActionBar(Resource.String.vacant_tables, Resource.Drawable.Vacant);
    }
    catch (System.Exception ex)
    {
        new AlertDialog.Builder(this)
            .SetPositiveButton("OK", (sender, args) => { })
            .SetMessage(ex.Message)
            .SetTitle("Error")
            .Show();
    }
}

3. 布局优化建议

你的activity_main.axml里同时有FrameLayout和ListView,如果每个Fragment内部已经包含了自己的ListView,这样会导致页面出现两个列表控件,建议简化布局,只保留用于承载Fragment的FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frameLayout1" />
</LinearLayout>

额外提醒:ActionBar Tabs已被弃用

顺带提一句,原生ActionBar的Tabs功能在Android 5.0(API 21)之后已经被官方弃用了,现在推荐使用TabLayout配合ViewPager来实现标签页功能,如果你是开发新版本的应用,建议迁移到这个方案,避免后续的兼容性问题。


内容的提问来源于stack exchange,提问作者Dot Net developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:56:46