View.findViewById()返回null [英] View.findViewById() returning null

查看:196
本文介绍了View.findViewById()返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code:

private void inflateMenu() {
    if (menu == null) {
        mFanView = (LinearLayout) findViewById(R.id.fanView);
        final View v = LayoutInflater.from(getContext()).inflate(
                R.layout.sidemenu, mFanView);
        menu = (MenuView) v.findViewById(R.id.listViewMenu);
    }
        menu.setColors(backgroundColor, selectedColor, unselectedColor);
}

在调试,V包含MenuView其ID等于 R.id.listViewMenu + 1 。当然,这将抛出一个menu.setColors NPE。

When debugging, v contains a MenuView whose id equals R.id.listViewMenu + 1. Of course this will throw a NPE on menu.setColors.

为什么R.id.listViewMenu包含的ID不会导致与XML该ID的看法?

Why does R.id.listViewMenu contain an ID that doesn't lead to the view with that ID in XML?

我试图清理我的项目,但它仍然保持不变。

I've tried to clean my project but it still stays the same.

编辑:发帖请求的文件

sidemenu.xml

sidemenu.xml

<com.ui.library.slidingmenu.MenuView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listViewMenu"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

编辑:问题解决了

Problem solved.

要有人来这里是因为findViewById返回null:清洁工程。如果它不工作,再次清理项目。如果它不工作,清理所有项目。直到它的作品。如果它不工作,然后问。

To anyone coming here because findViewById returns null: Clean the project. If it doesn't work, clean the project again. If it doesn't work, clean ALL your projects. Until it works. If it doesn't work, then ask.

推荐答案

试试这个:

(在Eclipse菜单栏)项目>清除

(In eclipse menu bar) Project > Clean

说明:有时候建设过程中,R.java类弄乱,并导致ID的错误引用。有时候,当我编译,图像突然变化,因为与之相关的ID被改变了。这可以用任何资源,布局,标识的等情况发生。

Explanation: sometimes during building, the R.java class messes up and causes wrong references to id's. Sometimes when I compile, an image suddenly changes because the id associated with it is changed too. This can happen with any resource, Layouts, Id's, etc.

要重新生成R.java类和修复违规行为的一种方法是通过清洗您的项目。

A way to regenerate the R.java class and fix irregularities is by cleaning your project.

希望帮助!

这篇关于View.findViewById()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆