简体   繁体   中英

FindViewByID() Can't find my ID (2 activities)

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

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.

  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM