SetSupportActionBar在visual studio和Xamarin中崩溃

本文关键字:Xamarin 崩溃 studio visual SetSupportActionBar | 更新日期: 2023-09-27 18:14:40

我正在尝试使用Xamarin和Visual Studio创建一个android应用程序。突然我得到一个奇怪的错误,而使用SetSupportActionBar函数。

这是我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:minHeight="?attr/actionBarSize"
      android:background="?attr/colorPrimary"
      app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
  <android.support.v4.widget.DrawerLayout
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      .
      .
      .
    </RelativeLayout>
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#818181"
        android:dividerHeight="1dp"
        android:background="#E3F2FD" />
  </android.support.v4.widget.DrawerLayout>
</LinearLayout>
这是我的c#文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.OS;
using Android.Widget;
using SupportToolBar = Android.Support.V7.Widget.Toolbar;
using Android.Support.V7.App;
using Android.Support.V4.Widget;
using Android.Locations;
using System.Net;
namespace IBuy
{
    [Activity(Label = "Main Screen", Theme = "@style/MyTheme")]
    public class MainScreen : ActionBarActivity, ILocationListener
    {
        private SupportToolBar mToolbar;
        private MyActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ListView mLeftDrawer;
        private int userID, storeID, permission;
        private LocationManager locationManager;
        private string deviceID = Build.Serial;
        private double currentX = 0, currentY = 0;
        private bool sent = false;
        private ProgressBar pBar;
        private TextView txt;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.MainScreen);
            Title = GetString(Resource.String.main_screen);
            try
            {
                mToolbar = FindViewById<SupportToolBar>(Resource.Id.toolbar);
                mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
                mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);
                SetSupportActionBar(mToolbar);
            }
            catch (Exception ex)
            {
                Console.WriteLine("@@@@@@@@@@@@@@@@@@@");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.InnerException);
                Console.WriteLine(ex.Data);
                Console.WriteLine(ex.Source);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.ToString());
                Console.WriteLine("#################");
            }
            mDrawerToggle = new MyActionBarDrawerToggle(this, mDrawerLayout, Resource.String.openDrawer, Resource.String.closeDrawer);
            mDrawerLayout.SetDrawerListener(mDrawerToggle);
            SupportActionBar.SetHomeButtonEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(true);
            mDrawerToggle.SyncState();
            .
            .
            .
        }
    }
}

奇怪的是,当涉及到这个命令时,应用程序崩溃了:SetSupportActionBar(mToolbar);

这是我得到的例外:

Exception of type 'Java.Lang.IllegalStateException' was thrown.
09-26 12:09:03.399 I/mono-stdout(13640): Exception of type 'Java.Lang.IllegalStateException' was thrown.
System.Collections.Generic.Dictionary`2[System.Object,System.Object]
09-26 12:09:05.849 I/mono-stdout(13640): System.Collections.Generic.Dictionary`2[System.Object,System.Object]
mscorlib
09-26 12:09:07.468 I/mono-stdout(13640): mscorlib
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
09-26 12:09:09.037 I/mono-stdout(13640):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
09-26 12:09:09.038 I/mono-stdout(13640):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 
  at Android.Support.V7.App.AppCompatActivity.SetSupportActionBar (Android.Support.V7.Widget.Toolbar toolbar) [0x00091] in <filename unknown>:0 
  at IBuy.MainScreen.OnCreate (Android.OS.Bundle bundle) [0x0005b] in C:'Users'איציק'Source'Workspaces'iBuy'IBuy'IBuy'Screens'MainScreen.cs:42 
  --- End of managed exception stack trace ---
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:193)
    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
    at md5f6ea2975de2355cb6a
09-26 12:09:09.038 I/mono-stdout(13640):   at Android.Support.V7.App.AppCompatActivity.SetSupportActionBar (Android.Support.V7.Widget.Toolbar toolbar) [0x00091] in <filename unknown>:0 
09-26 12:09:09.038 I/mono-stdout(13640):   at IBuy.MainScreen.OnCreate (Android.OS.Bundle bundle) [0x0005b] in C:'Users'איציק'Source'Workspaces'iBuy'IBuy'IBuy'Screens'MainScreen.cs:42 
574b5b4ea31e7d.MainScreen.n_onCreate(Native Method)
    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.onCreate(MainScreen.java:36)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
    at android.app.ActivityThread.access$900(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5289)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
09-26 12:09:09.038 I/mono-stdout(13640):   --- End of managed exception stack trace ---
09-26 12:09:09.038 I/mono-stdout(13640): java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
09-26 12:09:09.039 I/mono-stdout(13640):    at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:193)
09-26 12:09:09.039 I/mono-stdout(13640):    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
09-26 12:09:09.039 I/mono-stdout(13640):    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.n_onCreate(Native Method)
09-26 12:09:09.039 I/mono-stdout(13640):    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.onCreate(MainScreen.java:36)
09-26 12:09:09.040 I/mono-stdout(13640):    at android.app.Activity.performCreate(Activity.java:5990)
09-26 12:09:09.040 I/mono-stdout(13640):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-26 12:09:09.040 I/mono-stdout(13640):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
09-26 12:09:09.041 I/mono-stdout(13640):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
09-26 12:09:09.041 I/mono-stdout(13640):    at android.app.ActivityThread.access$900(ActivityThread.java:154)
09-26 12:09:09.042 I/mono-stdout(13640):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
09-26 12:09:09.042 I/mono-stdout(13640):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 12:09:09.042 I/mono-stdout(13640):    at android.os.Looper.loop(Looper.java:135)
09-26 12:09:09.042 I/mono-stdout(13640):    at android.app.ActivityThread.main(ActivityThread.java:5289)
09-26 12:09:09.042 I/mono-stdout(13640):    at java.lang.reflect.Method.invoke(Native Method)
09-26 12:09:09.042 I/mono-stdout(13640):    at java.lang.reflect.Method.invoke(Method.java:372)
09-26 12:09:09.042 I/mono-stdout(13640):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
09-26 12:09:09.042 I/mono-stdout(13640):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
09-26 12:09:10.509 I/mono-stdout(13640): Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
Java.Lang.IllegalStateException: Exception of type 'Java.Lang.IllegalStateException' was thrown.
09-26 12:09:10.515 I/mono-stdout(13640):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
09-26 12:09:10.517 I/mono-stdout(13640):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue* parms) [0x00084] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:1029 
  at Android.Support.V7.App.AppCompatActivity.SetSupportActionBar (Android.Support.V7.Widget.Toolbar toolbar) [0x00091] in <filename unknown>:0 
09-26 12:09:10.518 I/mono-stdout(13640):   at Android.Support.V7.App.AppCompatActivity.SetSupportActionBar (Android.Support.V7.Widget.Toolbar toolbar) [0x00091] in <filename unknown>:0 
  at IBuy.MainScreen.OnCreate (Android.OS.Bundle bundle) [0x0005b] in C:'Users'איציק'Source'Workspaces'iBuy'IBuy'IBuy'Screens'MainScreen.cs:42 
  --- End of managed exception stack trace ---
09-26 12:09:10.520 I/mono-stdout(13640):   at IBuy.MainScreen.OnCreate (Android.OS.Bundle bundle) [0x0005b] in C:'Users'איציק'Source'Workspaces'iBuy'IBuy'IBuy'Screens'MainScreen.cs:42 
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:193)
    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.n_onCreate(Native Method)
    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.onCreate(MainScreen.java:36)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
09-26 12:09:10.520 I/mono-stdout(13640):   --- End of managed exception stack trace ---
    at android.app.ActivityThread.access$900(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
09-26 12:09:10.523 I/mono-stdout(13640): java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
    at android.app.ActivityThread.main(ActivityThread.java:5289)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
09-26 12:09:10.523 I/mono-stdout(13640):    at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:193)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
09-26 12:09:10.524 I/mono-stdout(13640):    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:96)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
09-26 12:09:10.529 I/mono-stdout(13640):    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.n_onCreate(Native Method)
09-26 12:09:10.529 I/mono-stdout(13640):    at md5f6ea2975de2355cb6a574b5b4ea31e7d.MainScreen.onCreate(MainScreen.java:36)
09-26 12:09:10.530 I/mono-stdout(13640):    at android.app.Activity.performCreate(Activity.java:5990)
09-26 12:09:10.531 I/mono-stdout(13640):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
09-26 12:09:10.532 I/mono-stdout(13640):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309)
09-26 12:09:10.534 I/mono-stdout(13640):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
09-26 12:09:10.535 I/mono-stdout(13640):    at android.app.ActivityThread.access$900(ActivityThread.java:154)
09-26 12:09:10.536 I/mono-stdout(13640):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
09-26 12:09:10.537 I/mono-stdout(13640):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-26 12:09:10.539 I/mono-stdout(13640):    at android.os.Looper.loop(Looper.java:135)
09-26 12:09:10.543 I/mono-stdout(13640):    at java.lang.reflect.Method.invoke(Native Method)
09-26 12:09:10.544 I/mono-stdout(13640):    at java.lang.reflect.Method.invoke(Method.java:372)
09-26 12:09:10.546 I/mono-stdout(13640):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
09-26 12:09:10.547 I/mono-stdout(13640):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

它以前工作得很好,我不记得对它做了什么改变会使它崩溃。

有谁能给我点建议吗?

提前感谢!

SetSupportActionBar在visual studio和Xamarin中崩溃

看起来你的主题没有正确配置。

在主题中使用(参见相关答案)

<item name="windowActionBar">false</item>

或派生自没有操作栏的主题(参见相关答案),例如:

Theme.AppCompat.Light.NoActionBar