【问题标题】:CS0117 C# 'Resource.Id' does not contain a definition for "PhoneNumberText"CS0117 C#“Resource.Id”不包含“PhoneNumberText”的定义
【发布时间】:2016-06-29 14:57:56
【问题描述】:

我将 Visual Studio 2015 与 Xamarin 一起使用。 我将我的项目命名为“Phoneword” 我在 Xamarin 的网站上看到了此代码,例如教程/示例。

TranslateButtonCallButton 成员的错误相同。

MainActivity.cs

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Phoneword
{
[Activity(Label = "Phoneword", MainLauncher = true)]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Our code will go here
        // Get our UI controls from the loaded layout:
        EditText phoneNumberText = (EditText)FindViewById(Resource.Id.PhoneNumberText);
        Button translateButton = FindViewById<Button>(Resource.Id.TranslateButton);
        Button callButton = FindViewById<Button>(Resource.Id.CallButton);

        // Disable the "Call" button
        callButton.Enabled = false;

        // Add code to translate number
        string translatedNumber = string.Empty;

        translateButton.Click += (object sender, EventArgs e) =>
        {
            // Translate user's alphanumeric phone number to numeric
            translatedNumber = Core.PhonewordTranslator.ToNumber(phoneNumberText.Text);
            if (String.IsNullOrWhiteSpace(translatedNumber))
            {
                callButton.Text = "Call";
                callButton.Enabled = false;
            }
            else
            {
                callButton.Text = "Call " + translatedNumber;
                callButton.Enabled = true;
            }
        };

        callButton.Click += (object sender, EventArgs e) =>
        {
            // On "Call" button click, try to dial phone number.
            var callDialog = new AlertDialog.Builder(this);
            callDialog.SetMessage("Call " + translatedNumber + "?");
            callDialog.SetNeutralButton("Call", delegate {
                // Create intent to dial phone
                var callIntent = new Intent(Intent.ActionCall);
                callIntent.SetData(Android.Net.Uri.Parse("tel:" + translatedNumber));
                StartActivity(callIntent);
            });
            callDialog.SetNegativeButton("Cancel", delegate { });

            // Show the alert dialog to the user and wait for response.
            callDialog.Show();
        };
    }

}
}

资源/布局/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="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="Enter a Phoneword"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PhoneNumberText"
        android:text="1-855-XAMARIN" />
    <Button
        android:text="Translate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/TranslateButton" />
    <Button
        android:text="Call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CallButton" />
</LinearLayout>

我是 Android 开发的初学者,如果您需要其他信息,我会尝试添加更多详细信息。 请帮助我

【问题讨论】:

标签: c# android visual-studio xamarin xamarin.android


【解决方案1】:

我解决了我的问题! 从 Tools/Android SDK Manager 我卸载了:

  • Android SDK 构建工具 24
  • Android N (API 24)

我安装了:

  • Android 6.0 (API 23)
  • Android SDK 构建工具 23.0.3
  • Android SDK 构建工具 23.0.2

在我关闭 VS 并重新启动它之后...在解决方案资源管理器中,我选择了“清洁解决方案”,然后选择了“重建解决方案”。

【讨论】:

    【解决方案2】:

    这种事情在 Visual Studio 中经常发生,浪费了很多时间。

    试试……

    从项目中排除,然后添加现有项目

    经常工作,至少在上面的一些建议之前尝试更容易

    【讨论】:

      【解决方案3】:

      对我来说,问题是我在项目中工作了一段时间后,更改了默认创建的命名空间。

      【讨论】:

      • 是的,相同 - 直接在我的活动中更改命名空间,而无需通过项目进行“正确”重命名
      【解决方案4】:

      如果您有一个没有 AndroidResource 构建操作的值 XML 文件(有时 VS 会将其创建为 TransformFile),也可能会导致这种情况

      【讨论】:

      • 我知道,但在我的情况下,问题是由 sdk 构建工具引起的。还是谢谢你
      【解决方案5】:

      我有同样的错误,但我的解决方案本身仍然可以编译。

      虽然错误列表中到处都是错误错误,但仍然很烦人。

      试试这个:

      • 删除 .vs 文件夹

      • 清理解决方案,然后重新打开工作室,

      通过这些步骤,我不再收到错误错误。我建议任何有类似情况的人试试这个。

      【讨论】:

        【解决方案6】:

        按照教程Remote Notification with Firebase Cloud Messaging,我遇到了同样的问题,我不得不用引入资源的代码替换de“activity_main.axml”

        android:id="@+id/msgText"
        

        应用程序运行良好,但编译器在调用它的方法 MainActivity.OnCreate 中显示错误“resource.id 不包含 msgText 的定义”方式:

        this.msgText = FindViewById<TextView>(Resource.Id.msgText);
        

        在我关闭 VS 并重新启动它之后...在解决方案资源管理器中,我选择了“清理 > 解决方案”,然后选择了“重建解决方案”。

        为我工作。顺便说一句,我正在运行 VS2019

        【讨论】:

          【解决方案7】:

          我知道这可以追溯到 2014 年,这个问题遍布整个网络,并且在 2019 年仍然是一个问题。所有网站都无法为我解决这个问题,即使它位于自动生成的代理类。

          现在这似乎工作了,我使用按钮控件的动态 ID 而不是它的名称,即。按钮 dsdsd = FindViewById(2131230730);到目前为止一切顺利。

          编辑:确认这对我有用,触发我的委托函数。

          编辑:又一次更新,在添加另一个 UI 控件之后,按顺序在按钮之后,按钮名称现在显示出来,(intelisense)。我现在不用用身份证了。但是新控件没有出现。即使完全重建并关闭并重新打开项目/IDE。顺便说一句,使用 Xamarin Android 本机,而不是表单。

          【讨论】:

            【解决方案8】:

            当您的布局 xml 文件有错误(尚未发现)时,有时会发生此类错误。例如,如果您有属性重复或未闭合的引号(可能在您更改值时发生)。

            因此,包含错误的元素和以下所有元素将在您的项目中变得“不可见”。

            再次尝试检查错误。当错误消失时 - 元素自动变为“可见”!

            【讨论】:

              【解决方案9】:

              重新启动视觉工作室后它对我有用

              【讨论】:

                【解决方案10】:

                如果您的 xml 文件中没有我在下面提供的这三项内容,您如何转到与此活动文件相关的 xml 文件并添加它们。

                  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"
                

                您必须将 &lt;?xml version="1.0" encoding="utf-8"?&gt; 放在之后的相对或线性布局中,例如像这样

                <?xml version="1.0" encoding="utf-8"?>
                <RelativeLayout 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">
                </RelativeLayout>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-06-17
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2016-02-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多