无法在Xamarin.Android中添加导航抽屉活动。添加活动后,我无法导航到该活动

问题描述 投票:0回答:1

我是Xamarin.Android开发的新手。我想在我的应用程序中添加一个导航活动。我还使用导航抽屉活动在Android Studio中创建了一个示例项目。我已经在Xamarin.Android中创建了相同的脚本(Visual Studio 2019-v16.3.2与Xamarin v16.3.0.274)。因此,我在Xamarin.Android中创建了2个Activity,与Android Studio Project类似。在Android Studio项目中,我可以从主活动导航到其他活动,没有任何问题,但是,如果从主活动导航到第二个活动,则会出现错误

'此活动已经具有窗口装饰提供的操作栏。不要在您的主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false来代替使用工具栏。'

这是我的styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:versionCode="1" 
          android:versionName="1.0" 
          package="com.companyname.weatherapp">
  <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
  <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
  </application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

这是我的第二个活动布局(activity_favorites.xml)>] >>

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_favorites"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_favorites"
        app:menu="@menu/activity_favorites_drawer" />

</android.support.v4.widget.DrawerLayout>

这是我的第二活动脚本(FavoritesActivity.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Support.V7.App;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V4.View;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace WeatherApp
{
    [Activity(Label = "Favorites")]
    public class FavoritesActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_favorites);

            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar); // HERE I AM GETTING THE ERROR WHILE RUNNING: This Activity already has an action bar supplied...
            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.Click += FabOnClick;

            DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, /*toolbar, */Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
            drawer.AddDrawerListener(toggle);
            toggle.SyncState();

            NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.SetNavigationItemSelectedListener(this);
        }
        private void FabOnClick(object sender, EventArgs e)
        {
            View view = (View)sender;
            Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
                .SetAction("Action", (View.IOnClickListener)null).Show();
        }

        public bool OnNavigationItemSelected(IMenuItem item)
        {
            int id = item.ItemId;
            if (id == Resource.Id.nav_home)
            {
                Intent intent = new Intent(this, typeof(MainActivity));
                StartActivity(intent);
            }
            else if (id == Resource.Id.nav_slideshow)
            { }
            DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            drawer.CloseDrawer(GravityCompat.Start);
            return true;
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            this.MenuInflater.Inflate(Resource.Menu.menu_favorites, menu);
            return true;
        }
    }
}

请注意,我没有任何编译错误。每当我从导航抽屉中的MainActivity导航到“收藏夹”活动时,都会收到此错误。任何帮助将不胜感激。

我是Xamarin.Android开发的新手。我想在我的应用程序中添加一个导航活动。我还使用导航抽屉活动在Android Studio中创建了一个示例项目。我已经创建了...

c# android xamarin xamarin.forms xamarin.android
1个回答
0
投票

*尝试更改

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

© www.soinside.com 2019 - 2024. All rights reserved.