问题
I use Visual Studio 2015 with Xamarin. I'm naming my project "Phoneword" I see this code such as tutorial/example on Xamarin's site.
The same error for TranslateButton and CallButton members.
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();
};
}
}
}
Resource/Layout/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>
I'm beginner in Android development, if you need other information i'll try to add more detail. Please help me
回答1:
This kind of thing happens so often in Visual Studio, and wastes so much time.
Try...
Exclude from Project, then Add Existing Item
Works more often than not, much easier to at least try before some of the suggestions above
回答2:
I solved my problem! From Tools/Android SDK Manager I uninstalled:
- Android SDK Build-tools 24
- Android N (API 24)
and I installed:
- Android 6.0 (API 23)
- Android SDK Build-tools 23.0.3
- Android SDK Build-tools 23.0.2
After I closed VS and I restarted it... In the Solution Explorer I selected "Clean Solution" and after I selected "Rebuild Solution".
回答3:
The issue for me was that I changed the Namespace from the default that was created, after I had been working in the project for a bit.
回答4:
This can also be caused if you have a values XML file that doesn't have a build action of AndroidResource (sometimes VS will create it as a TransformFile)
回答5:
I have the same error, but my solution itself would still compile.
It's still annoying though to have the error list littered with false errors.
Try this:
delete the .vs folder
clean solution and then re-open studio,
With these steps I no longer received the false errors. I would recommend trying this for anyone who has a similar situation.
回答6:
I faced the same problem following the tutorial Remote Notification with Firebase Cloud Messaging, I one step I had to replace de "activity_main.axml" with a code that introduced the resource
android:id="@+id/msgText"
The application did run fine, but compiler showed the error "resource.id does not contain a definition for msgText", in method MainActivity.OnCreate which invokes it this way:
this.msgText = FindViewById<TextView>(Resource.Id.msgText);
After I closed VS and I restarted it... In the Solution Explorer I selected "Clean >Solution" and after I selected "Rebuild Solution".
worked for me. BTW, I'm running VS2019
回答7:
I know this goes back to 2014, this issue is all over the web and still an issue in 2019. Nothing would fix it for me from all the websites, could never get FindViewById to see the Button's name even though it's in the auto generated proxy class.
Now this seems to be working, I used the dynamic ID of the button control instead of it's name, .i.e. Button dsdsd = FindViewById(2131230730); So far so good.
Edit: Confirmed this works for me, firing my delegate function.
Edit: Yet another update, after adding another UI control, sequentially after the button, the button name is now showing up, (in intelisense). I don't have to use the ID now. The new control is not showing up though. Even with complete rebuild and closing and re-opening the project/IDE. BTW using Xamarin Android native, not forms.
回答8:
Sometimes such error happens when your layout xml file has errors (which haven't been spotted yet). For example, if you have attribute duplicate or unclosed quotes (could happen when you change value).
So the element which contains an error and all the following elements will become 'invisible' in your project.
Just try checking for errors once more. When errors are gone - elements become 'visible' automatically!
来源:https://stackoverflow.com/questions/38103271/cs0117-c-sharp-resource-id-does-not-contain-a-definition-for-phonenumbertext