Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 - Xamarin - Android - Getting "resource.id does not contain a definition for xxx" when I try to do anything in the .cs file

Adding Additional Activity .cs and Layout axml Using Visual Studio 2015.

I'm very new to Xamarin and Android development, but have been a developer for a few years using VB and now C#. I have a simple app on Android 4.2 that is getting more complicated as I go along. The simple matter us that I want to add an additional GpsAction.cs and corresponding Gps.axml layout to the project. It seems impossible to find the right combination syntax to achive this. I have a mainActivity with main.axml. In VS 2015 it's very simple to add new but I keep getting "resource.id does not contain a definition for" I would really appreciate your help with this

namespace AddCam
{
[Activity(Label = "GpsActivity")]
public class GpsActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.GpsLayout);

        string c = FindViewById<TextView>(**Resource.Id.textView1**).Text;
        // Create your application here
    }
}
like image 261
RobertM - Orlando Avatar asked Mar 12 '16 03:03

RobertM - Orlando


7 Answers

For people who are still facing this issue, the default Build Action of the layout file would be set to TransformFile. Select the layout, go to the Layout Properties, and in the properties pane, Select AndroidResource as your Build Action. Clean build your project and it should work.

like image 88
Ram Iyer Avatar answered Oct 01 '22 10:10

Ram Iyer


I changed the text field "id" from "@+id/imageView1" to "1", saved, rebuilt and changed it back to "@+id/imageView1", it fixed it. I would like to add, this whole problem came from

  1. Adding a new activity and layout.
  2. Using preexisting code from another app that I had.
  3. Copying and pasting code from the original app to the new Activity and Layout.

All fairly common stuff, the real problem seemed always to be adding any new Activities and Layouts to a main Activity. It can get very convoluted and with no (known to me) logical way to run down a problem with Xamarin. Don't get me wrong compared to 10 years ago (the last mobile app I tried to write) Xamarin is heaven. Good coding folks, now if I can only figure out why Keyword "this" is error-ring on the added Activity.cs

like image 23
RobertM - Orlando Avatar answered Oct 01 '22 11:10

RobertM - Orlando


Just add namespace like that Android.Resource.Id - it's resolve for me

like image 35
Serhii Horun Avatar answered Oct 01 '22 09:10

Serhii Horun


What did work for me (Visual Studio 2017, opening an old Xamarin project):

  1. Delete obj and bin folders, build.
  2. If errors, restart Visual Studio (I know the pain).
  3. Build again

Now the Resource will be visible (of course, if you defined it correctly).

like image 26
Exel Gamboa Avatar answered Oct 01 '22 10:10

Exel Gamboa


The best solution I have found is to build solution. Choose Build solution from Build menu (or Ctrl+Shift+B). This action will resolve the issue.

like image 33
Slackgate Avatar answered Oct 01 '22 09:10

Slackgate


You Should add set value forandroid:id="@+id/button1" in axml of app, then rebuild the project and try again. like thisButton button = (Button)FindViewById(Resource.Id.button1); .

like image 44
PL_Pathum Avatar answered Oct 01 '22 09:10

PL_Pathum


Check if you are missing these namespaces in your layout file -

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"
like image 31
Rupam Das Avatar answered Oct 01 '22 10:10

Rupam Das