简体   繁体   English

FindViewByID() 找不到我的 ID(2 个活动)

[英]FindViewByID() Can't find my ID (2 activities)

I am new to Android programming and I have a little issue.我是 Android 编程的新手,我有一个小问题。

So I have 2 Activities and 2 layouts.所以我有 2 个活动和 2 个布局。

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.问题是我的第二个活动中有一个按钮,当我尝试声明它时 FindViewById() 无法从第二个布局中找到 id。 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.我重新构建了应用程序并仔细检查了 ID - 它们匹配但只是函数 FindViewById() 找不到我的按钮的 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);示例:我的第二个布局中有一个按钮(第二个活动?我不知道说什么是正确的,因为我说我是 android 编程新手)按钮 ID 是 = 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?您是否在第二个活动的 XML 文件中声明了按钮? 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.如果您只在第一个活动中声明了它,那么 findviewbyid 不会显示错误,因为存在具有该 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"检查您的按钮 ID 是否名为“@+id/LoginButton”

  2. The correct spelling should be findViewById(R.id.LoginButton) not the way you typed in your question.正确的拼写应该是findViewById(R.id.LoginButton)而不是您输入问题的方式。

  3. Or Are you trying to get the View out of a Fragment?或者您是否试图从片段中获取视图? Try getView().findViewById(...);尝试 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.这是因为 Resource.Designer 无法成功创建,这是因为您的 Layout 中有错误,因此请始终注意检查所有属性是否定义良好。

In MenuList.axml:在 MenuList.axml 中:

  1. Change android:textColor="000" to android:textColor="#000" (most likely this is the problem)android:textColor="000"更改为android:textColor="#000" (很可能这就是问题所在)
  2. android:layout_height="210.5dp" do you really need those .5 dp? android:layout_height="210.5dp"你真的需要那些 .5 dp 吗? I suggest to put it to android:layout_height="210dp"我建议把它放在android:layout_height="210dp"
  3. Remove android:layout_marginBottom="0.0dp" the margin is anyway 0dp.删除android:layout_marginBottom="0.0dp"边距无论如何都是 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.要跟踪此类错误,请将您的构建置于诊断状态,以便您可以查看导致问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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