Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置SetContentView后xamarin forms应用程序中未处理的异常_Xamarin_Xamarin.forms_Xamarin.android - Fatal编程技术网

设置SetContentView后xamarin forms应用程序中未处理的异常

设置SetContentView后xamarin forms应用程序中未处理的异常,xamarin,xamarin.forms,xamarin.android,Xamarin,Xamarin.forms,Xamarin.android,我需要将我的应用程序图标放在工具栏的左侧,因此我在toolbar.axml中放置了一个ImageButton。我设置了SetContentView,这样我就可以使用FindViewById访问ImageButton,并收听click事件 但是在添加SetContentView行之后,我的应用程序会抛出未处理的异常 下面是我的Main.axml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=

我需要将我的应用程序图标放在工具栏的左侧,因此我在toolbar.axml中放置了一个
ImageButton
。我设置了
SetContentView
,这样我就可以使用
FindViewById
访问
ImageButton
,并收听click事件

但是在添加
SetContentView
行之后,我的应用程序会抛出未处理的异常

下面是我的Main.axml:

<?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" >

    <android.support.v7.widget.Toolbar 
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary"
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
       android:popupTheme="@style/ThemeOverlay.AppCompat.Light">

   <ImageButton
       android:src="@drawable/appicon"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary"
       android:id="@+id/homeicon" />
   </android.support.v7.widget.Toolbar>

</LinearLayout>

我的MainActivity.cs如下所示:

[Activity(Label = "XamarinToolBar", Icon = "@drawable/icon", Theme = 
"@style/MainTheme", MainLauncher = true, ConfigurationChanges = 
ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : 
        global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            //TabLayoutResource = Resource.Layout.Tabbar;
            //ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            ImageButton homeButton = (ImageButton)FindViewById<ImageButton>(Resource.Id.homeicon);
            homeButton.Click += HomeButton_Click;

            global::Xamarin.Forms.Forms.Init(this, bundle);
            LoadApplication(new App());
        }

        private void HomeButton_Click(object sender, EventArgs e)
        {

        }
    }
[活动(Label=“XamarinToolBar”,Icon=“@drawable/Icon”,主题=
“@style/MainTheme”,MainLauncher=true,ConfigurationChanges=
ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
公开课活动:
全局::Xamarin.Forms.Platform.Android.formsappactivity
{
创建时受保护的覆盖无效(捆绑包)
{
//TabLayoutResource=Resource.Layout.Tabbar;
//ToolbarResource=Resource.Layout.Toolbar;
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ImageButton homeButton=(ImageButton)FindViewById(Resource.Id.homeicon);
主页按钮。单击+=主页按钮\u单击;
全局::Xamarin.Forms.Forms.Init(这个,bundle);
加载应用程序(新应用程序());
}
私有void HomeButton_单击(对象发送者,事件参数e)
{
}
}

我认为您正在尝试混合和匹配xamarin.forms“FormsAppCompatActivity”,并使其行为类似于正常的android活动,但情况不应该如此,因为xamarin.forms“FormsAppCompatActivity”通过使用xaml页面(例如ContentPage、MasterDetailPage等)有自己的底层呈现引擎。尝试创建一个Android项目

问题在于:

公开课活动: 全局::Xamarin.Forms.Platform.Android.formsappactivity

您正在将xamarin表单FormsAppCompatActivity与AppCompactActivity混合,并从cs文件中删除这两行

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
我希望有帮助

祝你好运