Android FindViewById() 没有找到视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11467760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:46:47  来源:igfitidea点击:

FindViewById() not finding View

androidandroid-layout

提问by Fran

Just added a new button to my already-working-fine layout, but the findViewById function seems to be angry with something I don't get to understand.

刚刚在我已经可以正常工作的布局中添加了一个新按钮,但是 findViewById 函数似乎对我无法理解的东西感到生气。

Here's a bit of the layout:

这里有一些布局:

<LinearLayout
        ...
    >
    <ListView
        android:id="@+id/my_lovely_list"
        android:layout_width="fill_parent"
        android:layout_weight="1" 
        />

    <Button
        android:id="@+id/my_lovely_butt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_b"
        android:layout_weight="0"
        android:clickable="true"
        />

</LinearLayout>

And here's a bit of the coding:

这是一些编码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
    list_o = (ListView)findViewById(R.id.my_lovely_list);
    butt_o = (Button)findViewById(R.id.my_lovely_butt);
    ...
}

So, the big mistery is that the ListView is found without any problem, but the Button won't by any means. I've already tried cleaning the Proyect, and look throught the posts I've found here... but still don't get to find the problem! Any thoughts?

所以,最大的谜团是 ListView 没有任何问题,但 Button 无论如何都不会。我已经尝试清理 Proyect,并查看我在这里找到的帖子......但仍然没有找到问题所在!有什么想法吗?

回答by MAC

import yourpackagename.R;

instead of android.R;

代替 android.R;

import Rof your package

导入R你的包

Also Cleanyour project that will refreshyour entire project then you will also find IDof button also

同时Clean你的项目,将刷新整个项目,那么你还可以找到ID按钮也

回答by An-droid

Some times the file R is not regenetated. Try :

有时文件 R 不会重新生成。尝试 :

  • Clean your project
  • Rebuild it
  • Manage to go back to a version that compiled and retry adding your elements one by one and compile each time to check the R file
  • Finnally the last option is to close/reopen eclipse yep some bugs can't be overcome other way ^^"
  • 清理你的项目
  • 重建它
  • 设法回到一个编译过的版本并重试一个一个地添加你的元素,每次编译以检查 R 文件
  • 最后最后一个选项是关闭/重新打开 eclipse 是的,有些错误无法通过其他方式克服 ^^”

No worry that's is just some common eclipse/android bug it happen often (Well some other times it can also be a small syntax error somewhere in your project but i assume that you already checked that x)

不用担心,这只是一些常见的 eclipse/android 错误,它经常发生(有时它也可能是您项目中某处的一个小语法错误,但我假设您已经检查过 x)

Good luck

祝你好运

回答by gaara87

  1. Clean your project in Eclipse.
  2. Make sure the id is not duplicated
  3. Remove the button, save and add the button again and build the code.
  4. Make sure the correct R file is imported.
  1. 在 Eclipse 中清理您的项目。
  2. 确保 id 没有重复
  3. 删除按钮,保存并再次添加按钮并构建代码。
  4. 确保导入了正确的 R 文件。

If its a syntax related problem, then its got to do with compile time stuff.

如果它是一个语法相关的问题,那么它与编译时的东西有关。

If its a problem when the code executes, make sure the layout is inflated. If the listview is found, then the button also must definitely be found.

如果代码执行时出现问题,请确保布局已膨胀。如果找到列表视图,那么按钮也一定要找到。

回答by Mohammad Naim Dahee

Make sure to go through these processes.

确保完成这些过程。

1: Make sure you imported your project R file.

1:确保您导入了您的项目 R 文件。

import packagename.R

2: Clean your project

2:清理你的项目

enter image description here

在此处输入图片说明

3: If the above two solutions doesn't work, do invalidate your cache and restart enter image description hereenter image description here

3:如果以上两种解决方案都不起作用,请使您的缓存无效并重新启动 在此处输入图片说明在此处输入图片说明

回答by Rocky Inde

In Android Studio Build-> Clean Projectresolved the problem for me.

在 Android Studio Build->Clean Project为我解决了这个问题。

回答by guoshichao

while, just by clean you project, rebuild it, or import the R file manually, it might a sad thing to find the id of specified view component still cannot find by the activity. and then, if you check the detailed content of the R file, you will find the id has not generate, and then, what you need to check, if there are something wrong with your own /res file, for example, the name of the layout file, drawable file name, or the variant name of the menu file .etc. all of these could just make the R file cannot generate correctly, or even worse, the R file does not generate out at all.

同时,仅通过清理您的项目、重建它或手动导入 R 文件,找到指定视图组件的 id 仍然无法被活动找到可能是一件可悲的事情。然后,如果你查看R文件的详细内容,你会发现id没有生成,然后,你需要检查的是,你自己的/res文件是否有问题,例如,名称布局文件、可绘制文件名或菜单文件的变体名称等。所有这些只会使R文件无法正确生成,甚至更糟的是,R文件根本不生成。