13

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
    }
}
Community
  • 1
  • 1
RobertM - Orlando
  • 213
  • 1
  • 3
  • 9
  • 3
    Xamarin is notoriously buggy, especially with layouts. Do a full rebuild and see if that helps. – Sami Kuhmonen Mar 12 '16 at 07:15
  • You are correct Sami, I was doing cleans and rebuilds over and over. So this morning I logged on and low and behold I had no error. So now I'm crazy with curiosity and have to find out what I did right. Turns out that if I changed the text field "id" saved, rebuilt and changed it back it fixed it. I'm marking your answer correct. Thanks a bunch - misery loves company – RobertM - Orlando Mar 12 '16 at 21:15
  • Gusman- I should have added the axml in the original. Before I realized it I had over written the original with attempts to fix it. I stared at it for a long time and it was correctly appearing that was the mystery and the exact same code was working in another app this was literally a cut and paste. But next time I will post the axml regardless. Thanks for the response. – RobertM - Orlando Mar 13 '16 at 16:13
  • I had one where the problem was an invalid XML syntax: a . (dot) somewhere after a closing /> . As there was no message about it, and a . is barely visible ........ it takes tome to find it. – Martin Jan 13 '19 at 15:25

17 Answers17

8

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.

Ram Iyer
  • 1,621
  • 1
  • 23
  • 25
7

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

Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
RobertM - Orlando
  • 213
  • 1
  • 3
  • 9
4

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

  • Thank you a lot! I've just added `public partial class Id { }` inside class Resource and rebuilt the project. Now all `Resource.Id.____` works well – Pavel Apr 24 '17 at 11:24
  • This one, done along with clean rebuild *and* [RoberM's answer](https://stackoverflow.com/a/35963477/479251) did the work. VS 2017 + Xamarin Forms 3.6.0 – Pac0 Jun 05 '19 at 09:27
3

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).

Exel Gamboa
  • 936
  • 1
  • 14
  • 25
1

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.

mx0
  • 6,445
  • 12
  • 49
  • 54
Slackgate
  • 15
  • 5
1

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); .

PL_Pathum
  • 173
  • 1
  • 11
1

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"
Rupam Das
  • 373
  • 9
  • 19
0

Cleared Main.axml page and Reset and it is working.

Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
0

My problem was I was trying to debug Xamarin Android project (native) using Xamarin Live Player, connecting using USB cable and selecting my device for debugging solved the problem

mohas
  • 1,911
  • 1
  • 16
  • 20
0

Make sure your axml is well formed and rebuild the solution it should work, if you continue facing the same issue then remove axml and add it again then build the solution.

0

In Visual Studio 2019 when you add a new Android Layout to the Project it is added as .xml file. I already had some created earlier layouts in Resources/layout folder with extension .axml (not .xml) and for me changing the extension .xml -> .axml worked.

maniacz
  • 9
  • 2
0

I understand that this issue is very old, but I've run into it as well in Visual Studio 2019, and have found a solution.

The issue occurs, for me, when adding an element to the layout and then attempting to add code. The issue appears to be related to the way the project is built.

Add the element to your layout, then build your project before adding any additional code. Apparently Resource.Id does not update with additional members until it's built, and attempting to refer to the new member in the code before Resource.Id recognizes it prevents the project from being built.

0

Like Maniacz said, in VS 2019 I just had to change the XML to axml extensión to the layout in layout folder under resources

0

For anyone else looking for an answer despite running clean/build/rebuild which didn't work for me:

I had freshly installed a number of tools for xamarin development in VS. Though a build/rebuild may have actually worked, in my case I believe what also fixed it was closing and re-opening VS. I'm pretty sure I had a few issues, primarily stemming from newly installed tools (android SDKs in my case) requiring VS to be restarted. If you're working on a project already having installed the tools you need, try as others have said - build/rebuild.

mbuchok
  • 391
  • 6
  • 18
0

(VS 2019) I did the previous solutions and nothing, I had the same problem and the solution for me was to remove '&' from the text of TextView, I was trying to Set the text to "text&text" even &&(I though it was like mnemonics as in WindowsForms) didn't work so I had to remove it and it fixed.

Ebrahim ElSayed
  • 157
  • 2
  • 10
0

I had to modify Build Action for my layout file and re set the original Build Action as it was previously set. This made my visual studio to regenerate the resource ids in Resource.designer.cs file.

Step #1:
Go to properties of the layout file that is missing its ids and click the Build Action DropDown. Properties Of the layout file

Step #2:
Select something from the dropdown other than AndroidResource.

Step #3:
Reselect AndroidResource from that Build Action.

Now, you will have your Resource.designer.cs file regenerated and it will have the reference ids to your controls in the layout file.

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42
-1

Check the Resource.Designer class file. There will be a class like public partial class Id. There the integer IDs of the controls are written. Use them instead of Resource.Id