繁体   English   中英

CS0117 C# “Resource.Id”不包含“PhoneNumberText”的定义

[英]CS0117 C# 'Resource.Id' does not contain a definition for "PhoneNumberText"

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

TranslateButtonCallButton成员出现相同的错误。

主活动.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 开发的初学者,如果您需要其他信息,我会尝试添加更多细节。 帮助我

我解决了我的问题! 从我卸载的工具/Android SDK 管理器:

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

我安装了:

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

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

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

试试...

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

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

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

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

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

尽管错误列表中充斥着错误的错误,但仍然很烦人。

试试这个:

  • 删除 .vs 文件夹

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

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

在教程Remote Notification with Firebase Cloud Messaging 之后,我遇到了同样的问题,我必须将 de "activity_main.axml" 替换为引入资源的代码

android:id="@+id/msgText"

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

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

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

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

我知道这可以追溯到 2014 年,这个问题遍布整个网络,并且在 2019 年仍然是一个问题。没有什么可以从所有网站上为我修复它,即使它在自动生成的代理中,也永远无法让 FindViewById 看到按钮的名称类。

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

编辑:确认这对我有用,触发了我的委托功能。

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

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

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

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

重新启动visual studio后它对我有用

你如何将 go 添加到与此活动文件相关的 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"

您必须将<?xml version="1.0" encoding="utf-8"?>放在相对或线性布局之后,例如像这样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM