Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.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
C# FindViewById返回null(只有一个,在OnCreate内部和SetContentView之后)_C#_Android_Xamarin_Xamarin.android - Fatal编程技术网

C# FindViewById返回null(只有一个,在OnCreate内部和SetContentView之后)

C# FindViewById返回null(只有一个,在OnCreate内部和SetContentView之后),c#,android,xamarin,xamarin.android,C#,Android,Xamarin,Xamarin.android,我试着在自己创建的工具栏中放置一个中心标题。它与基本工具栏完全一样工作,但出于不同的目的,我不得不切换到Android.Support.V7,现在由于FindViewById返回null,它不再工作了 这是我的代码:(在Xamarin.Android上工作) MainPage.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/ap

我试着在自己创建的工具栏中放置一个中心标题。它与基本工具栏完全一样工作,但出于不同的目的,我不得不切换到Android.Support.V7,现在由于
FindViewById
返回
null
,它不再工作了

这是我的代码:(在Xamarin.Android上工作)

MainPage.axml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbarMain"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorAccent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bitmap_homepage" />
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/ListSpaces"
            android:focusable="false" />
    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8" ?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbarMain"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

  <TextView
      android:id="@+id/toolbarMain_Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" />

</android.support.v7.widget.Toolbar>

toolbarMain.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbarMain"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorAccent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:src="@drawable/bitmap_homepage" />
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:id="@+id/ListSpaces"
            android:focusable="false" />
    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8" ?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbarMain"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

  <TextView
      android:id="@+id/toolbarMain_Name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center" />

</android.support.v7.widget.Toolbar>

MainPageActivity.cs

public static string TitleFolder = "Welcome";

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

            SetContentView(Resource.Layout.MainPage);

            //Put toolbar
            var toolbar = FindViewById<Toolbar>(Resource.Id.toolbarMain);
            var ToolTitle = FindViewById<TextView>(Resource.Id.toolbarMain_Name);
            SetSupportActionBar(toolbar);
            SupportActionBar.SetDisplayShowTitleEnabled(false);
            ToolTitle.Text = TitleFolder;
            }
公共静态字符串TitleFolder=“欢迎”;
创建时受保护的覆盖无效(Bundle savedInstanceState){
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.MainPage);
//放置工具栏
var toolbar=findviewbyd(Resource.Id.toolbarMain);
var ToolTitle=findviewbyd(Resource.Id.toolbarMain\u Name);
设置支持操作栏(工具栏);
SupportActionBar.SetDisplayShowTitleEnabled(假);
ToolTitle.Text=标题页;
}
Visual Studio给我的错误是

System.NullReferenceException:对象引用未设置为实例 指一个物体

查看变量时,程序会在Ressource.Designer中查找所有ID,但将null返回到
Tooltitle
,但
toolbar
是绝对正确的

有什么想法吗?它慢慢地让我发疯,因为它在标准工具栏上工作得非常好。。。
非常感谢

V7支持工具栏有不同的设置,请拨打:

setSupportActionBar(findViewById(R.id.toolbarMain))

查看详细信息。

您正在将contentview设置为MainPage.xml,但toolbarMain\u名称不是该布局的元素。您需要将textview添加到MainPage.xml中,或者用include语句替换工具栏声明

<include layout="@layout/your_toolbar_layout_name"/>


成功了,非常感谢!我在第一个工具栏上有include标记,但我在使用AppCompat再次创建时删除了它。我一直在关注官方的Xamarin文档,但我想我在更改代码时做得不对谢谢你的回答,我遵循了Cody W的答案。因为我将使用工具栏进行不同的活动,所以它对我来说更干净。但你的解决方案似乎是一个很好的解决办法,但这并不是我想要解决的问题,你的回答似乎对我不起作用,但无论如何还是要谢谢你