Resource.Id не содержит определения для myRecyclerView (Resource.Id.XXX) - PullRequest
0 голосов
/ 31 октября 2019

Я новичок в Xamarin и Firebase, и мой идентификатор myRecyclerView не может быть вызван в CS. Я пытался найти viewbyid через ресурсы, но я не могу назвать идентификатор. Мой код ниже.

это код для дизайна. activity_main.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/toolbar1"
        android:layout_height = "wrap_content"
        android:layout_width = "match_parent"
        android:background = "?android:attr/colorPrimary"
        android:minHeight = "?android:attr/actionBarSize"
        android:theme = "@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
        android:elevation = "4dp">

        <RelativeLayout
            android:layout_height = "wrap_content"
            android:layout_width = "match_parent">

            <TextView
             android:layout_centerVertical = "true"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text = "Employee"
             android:gravity = "center"
             android:textStyle = "bold"
             android:textColor = "#fff"
             android:textSize = "18sp"/>

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id = "@id/searchButton"
                android:layout_alignParentLeft = "true"
                android:src = "@drawable/search"
                android:tint = "#fff"
                android:layout_marginLeft = "10dp"/>

            <ImageView
                android:layout_width="30dp"
                android:layout_height="20dp"
                android:id = "@id/addButton"
                android:layout_alignParentRight = "true"
                android:src = "@drawable/add"
                android:tint = "#fff"
                android:layout_marginLeft = "10dp"/>

        </RelativeLayout>

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

    <EditText
        android:visibility = "gone"
        android:id = "@id/searchText"
        android:layout_marginLeft = "5dp"
        android:layout_marginRight = "5dp"
        android:layout_marginBottom = "5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint = "Search"/>


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars = "vertical"
        android:background = "#e2e2e2"
        android:id = "@+id/myRecyclerView"/>

    </LinearLayout>

и это мой код в MainActivity.cs

    using Android.App;
    using Android.OS;
    using Android.Support.V7.App;
    using Android.Runtime;
    using Android.Widget;
    using Android.Support.V7.Widget;

    namespace FirebaseSampleApp
    {
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        ImageView addButton;
        ImageView searchButton;
        EditText searchText;
        RecyclerView myRecyclerView;


        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
            // Set our view from the "main" layout resource

            SetContentView(Resource.Layout.activity_main);
            myRecyclerView = (RecyclerView)FindViewById(Resource.Id.myRecyclerView);

        }


        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, 
    [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, 
    grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    }

Может кто-нибудь сказать мне, что не так с моими кодами? Заранее спасибо!

Ответы [ 2 ]

0 голосов
/ 04 ноября 2019

Я нашел решение. Просто убедитесь, что все ваши самородки обновлены в одних и тех же версиях, и перестройте решение.

0 голосов
/ 31 октября 2019

Если файл макета (activity_main.axml) не верен, то файл Resource.designer.cs (который включает в себя сгенерированный идентификатор видов ресурсов, например, идентификатор представления, файл макета и т. Д.) Не может быть сгенерирован правильно.

Мы обнаружили, что в вашем activity_main.axml

1.change

android:orientation = vertical

на

android:orientation = "vertical"

2. когда выдобавьте id к элементу управления, просто добавьте +, вот так:

 android:id="@+id/searchButton"

Not

android:id = "@id/searchButton"

Между тем, убедитесь, что используемые вами изображения существуют в нужном месте. (например, drawable папка). (android:src ="@drawable/search" и android:src = "@drawable/add")

Примечание :

После того, как мы кодируем, мы можем просто создать приложение, а затембудет иметь несколько журналов ошибок, если у нашего кода есть какие-то проблемы.

Таким образом, весь файл макета выглядит так:

<?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/toolbar1"
    android:layout_height = "wrap_content"
    android:layout_width = "match_parent"
    android:background = "?android:attr/colorPrimary"
    android:minHeight = "?android:attr/actionBarSize"
    android:theme = "@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar"
    android:elevation = "4dp">


    <RelativeLayout
        android:layout_height = "wrap_content"
        android:layout_width = "match_parent">
        <TextView
         android:layout_centerVertical = "true"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text = "Employee"
         android:gravity = "center"
         android:textStyle = "bold"
         android:textColor = "#fff"
         android:textSize = "18sp"/>

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/searchButton"
            android:layout_alignParentLeft = "true"
            android:src ="@drawable/search"
            android:tint = "#fff"
            android:layout_marginLeft = "10dp"/>

        <ImageView
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:id="@+id/addButton"
            android:layout_alignParentRight = "true"
            android:src = "@drawable/add"
            android:tint = "#fff"
            android:layout_marginLeft = "10dp"/>
    </RelativeLayout>

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

<EditText
    android:visibility = "gone"
    android:id = "@+id/searchText"
    android:layout_marginLeft = "5dp"
    android:layout_marginRight = "5dp"
    android:layout_marginBottom = "5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint = "Search"/>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars = "vertical"
    android:background = "#e2e2e2"
    android:id = "@+id/myRecyclerView"/>
 </LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...