【问题标题】:Create instance of ActionBar Xamarin Android创建 ActionBar Xamarin Android 的实例
【发布时间】:2014-07-25 08:59:28
【问题描述】:

我在我的应用程序中使用了 ActionBar。我做了这样的事情:-

ActionBar.SetDisplayHomeAsUpEnabled(true);
ActionBar.SetHomeButtonEnabled(true);

但我收到如下编译错误:-

A object reference is required for the non-static field, method, or property 'Android.Support.V7.App.ActionBar.SetDisplayHomeAsUpEnabled(bool)'

如何创建 ActionBar 的实例,因为它是一个抽象类?

编辑:-

使用 waquaslam 的解决方案,我这样做了:-

public class Homescreen : ActionBarActivity
{

    private DrawerLayout _drawer;
    private MyActionBarDrawerToggle _drawerToggle;
    private ListView _drawerList;
    private static ActionBar ActionBar ;

    private string _drawerTitle;
    private string _title;
    private string[] _planetTitles;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SupportRequestWindowFeature(WindowCompat.FeatureActionBar);



        SetContentView(Resource.Layout.homescreen);




        _title = _drawerTitle = Title;
        _planetTitles = Resources.GetStringArray(Resource.Array.TitlesArray);
        _drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        _drawerList = FindViewById<ListView>(Resource.Id.left_drawer);

        _drawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityFlags.Left);

        _drawerList.Adapter = new ArrayAdapter<string>(this,
            Resource.Layout.drawerlistitem, _planetTitles);
        _drawerList.ItemClick += (sender, args) => SelectItem(args.Position);






        SupportActionBar.SetDisplayHomeAsUpEnabled(true);

        SupportActionBar.SetHomeButtonEnabled(true);

        //DrawerToggle is the animation that happens with the indicator next to the
        //ActionBar icon. 
        _drawerToggle = new MyActionBarDrawerToggle(this, _drawer,
                                                  Resource.Drawable.ic_drawer_light,
                                                  Resource.String.DrawerOpen,
                                                  Resource.String.DrawerClose);

        //You can alternatively use _drawer.DrawerClosed here
        _drawerToggle.DrawerClosed += delegate
        {
            ActionBar.Title = _title;
            //InvalidateOptionsMenu();
        };

        //You can alternatively use _drawer.DrawerOpened here
        _drawerToggle.DrawerOpened += delegate
        {
            ActionBar.Title = _drawerTitle;
            // InvalidateOptionsMenu();
        };

        _drawer.SetDrawerListener(_drawerToggle);

        if (null == savedInstanceState)
            SelectItem(0);

    }

现在我收到了NoClassDefinitionFoundError

【问题讨论】:

    标签: c# android xamarin instance abstract-class


    【解决方案1】:

    如果您使用的是 support.V7,请确保您的活动继承自 ActionBarActivity

    为了在兼容性库中获取 ActionBar 的引用,您必须需要使用以下内容:

    SupportActionBar.SetDisplayHomeAsUpEnabled(true);
    SupportActionBar.SetHomeButtonEnabled(true);
    

    有关更多信息,请阅读this,其中的示例基于操作栏中的选项卡,但使用了支持库。

    【讨论】:

    • @waqslam 我试过你的解决方案。我可以摆脱这个问题,但现在我面临另一个问题。请在base.OnCreate(savedInstanceState); 行查看我的编辑
    • 你不应该更新问题并完全改变它的方向。实际上发布另一个问题,因为您的两个问题彼此无关。无论如何,如果您也从 logcat 发布异常堆栈跟踪,调查会更有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多