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

时间:2016-06-29 14:57:56

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

我将Visual Studio 2015与Xamarin一起使用。 我正在命名我的项目“Phoneword” 我在Xamarin的网站上看到了这个代码,例如tutorial / example。

TranslateButton CallButton 成员的错误相同。

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开发中初学者,如果您需要其他信息,我会尝试添加更多详细信息。 请帮助我

9 个答案:

答案 0 :(得分:2)

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

  • Android SDK Build-tools 24
  • Android N(API 24)

我安装了:

  • Android 6.0(API 23)
  • Android SDK Build-tools 23.0.3
  • Android SDK Build-tools 23.0.2

关闭VS并重新启动后...在解决方案资源管理器中,我选择了#34; Clean Solution&#34;在我选择&#34; Rebuild Solution&#34;。

之后

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

尝试...

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

虽然错误列表中包含错误错误,但仍然很烦人。

试试这个:

  • 删除.vs文件夹

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

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

答案 5 :(得分:0)

在教程Remote Notification with Firebase Cloud Messaging之后,我遇到了同样的问题,第一步,我必须用引入资源的代码替换“ activity_main.axml”

android:id="@+id/msgText"

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

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

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

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

答案 6 :(得分:0)

我知道这可以追溯到2014年,这个问题已经遍及整个网络,并且在2019年仍然是一个问题。没有什么办法可以解决所有网站上的问题,即使它位于目录中,也永远无法让FindViewById看到Button的名称。自动生成的代理类。

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

编辑:确认这对我有效,并激活了我的委托功能。

编辑:在添加了另一个UI控件之后,又进行了一次更新,在按钮之后按顺序显示了按钮名称(以intelisense表示)。我现在不必使用ID。新控件不会出现。即使完全重建并关闭并重新打开项目/ IDE,也是如此。 BTW使用Xamarin Android本机,而不是表格。

答案 7 :(得分:0)

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

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

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

答案 8 :(得分:0)

在重新启动Visual Studio后对我有用

相关问题