-2

I am new to Android programming and I have a little issue.

So I have 2 Activities and 2 layouts.

The problem is that I have a button in my second activity and when I try to declare it FindViewById() can't find the id from the second layout. I re-built the app and I double checked the IDs - they match but simply the function FindViewById() does not find my button's id. Example: I have a button in my second layout(second activity? i don't know what is correct to say, as I said im new to android programming) The button ID is = LoginButton, When I go to my second activity and type Button LoginButton FindViewByID(Resources.Id.LoginButton); The function does not find it :/ Im so confused. Please help me

Lyubo Ivailov
  • 11
  • 2
  • 5

3 Answers3

0

Have you declared the button in the XML file of the second activity? If you have only declared it in the first activity, then findviewbyid won't show an error since there exists an element with that ID. But it won't run (will throw an error) simply because it won't be able to locate that specific element in your (second) activity.

  • Yes I have everything declared. The problem is that the function FindViewById() doesnt find ids from my second activity. – Lyubo Ivailov Sep 25 '17 at 19:41
  • That probably means that the resources file (R file) isn't loaded in that activity. Or it could even be a typo. Try rebuilding your project? – Aditya Mulgundkar Sep 25 '17 at 19:42
  • Yes i tried... everything is loading perfectly but the func FindViewById() doesnt find the id from the second layout – Lyubo Ivailov Sep 25 '17 at 19:48
  • Could you add your code to the question. Just so that the rest of us can verify. Also please elaborate on what you mean by "the function won't work". Any errors or exceptions? – Aditya Mulgundkar Sep 25 '17 at 19:51
  • Here are some pictures which should explain: https://gyazo.com/62b3ed7d199ff0e6242069a8526599f3 https://gyazo.com/bad69ceedcdc6e285921ba2119946e79 – Lyubo Ivailov Sep 25 '17 at 20:00
  • Looks like a common bug with Xamarin... https://stackoverflow.com/questions/35953540/visual-studio-2015-xamarin-android-getting-resource-id-does-not-contain-a Like many people have said on that post, rename the ID of your button (LoginButton) to something else and then build the project, then change the ID back and build again. – Aditya Mulgundkar Sep 26 '17 at 05:19
0
  1. Check if your button's ID is named "@+id/LoginButton"

  2. The correct spelling should be findViewById(R.id.LoginButton) not the way you typed in your question.

  3. Or Are you trying to get the View out of a Fragment? Try getView().findViewById(...);

  4. if that not works, give us some code, sounds like a typo somewhere.

Eirik Fesker
  • 328
  • 1
  • 12
  • Yes it is named LoginButton, I have everything declared. Just the function FindViewById() does not find IDs from my second activity. – Lyubo Ivailov Sep 25 '17 at 19:42
  • Dude its not a spelling issue, its all ok. The problem is that on my second Layout I have a button with ID: LoginButon, and when I go to the second activity.cs and try to declare the button, the function findviewbyid just doesnt find the ID. I tried Re-Building and it still doesnt work. On the other hand if I do this on my first activity it all works, I think that the function just searches for id's from the first layout and not from the second. Here are some screenshots: https://gyazo.com/62b3ed7d199ff0e6242069a8526599f3 https://gyazo.com/bad69ceedcdc6e285921ba2119946e79 – Lyubo Ivailov Sep 25 '17 at 19:59
  • Try to rename the Button and rebuild. Rename the MenuList and rebuild. Xamarin is sometimes buggy as hell. Make a new project and paste your code there. Or stop using Xamarin, i think your code is correct. But i dont understand why you have two Login-Buttons. Do they have the same ID? Usually there is only one login layout with a login_btn. I just dont get the logic. even if you have two different layouts, you cant assign the same id-name for two buttons. – Eirik Fesker Sep 26 '17 at 09:02
0

This is because the Resource.Designer can not be created successfully and that is because you have errors in your Layout, so always be aware to check if all properties are defined good.

In MenuList.axml:

  1. Change android:textColor="000" to android:textColor="#000" (most likely this is the problem)
  2. android:layout_height="210.5dp" do you really need those .5 dp? I suggest to put it to android:layout_height="210dp"
  3. Remove android:layout_marginBottom="0.0dp" the margin is anyway 0dp.

After doing this changes Clean and Rebuild the project and see if the error disappear.

To track this kind of errors put your Build in diagnostic so you can see what is making a problem.

Stefanija
  • 1,388
  • 2
  • 12
  • 25