分享

findViewById()返回 null

 univasity 2011-12-20
 1 第一种情况

通过findViewById()获取一个Button时一直返回null值,百思不得其解。最后发现是因为在layout文件中使用了id的旧风格,如下所示:

<Button id="@+id/btn_ok" />

改成android:id就成功了。

<Button android:id="@+id/btn_ok" />
2 第二种情况:

原來把findViewById的動作移到了 setContentView(R.layout.main);之前 在Android如果在使用setContentView把顯示元件創造出來之前 findViewById是找不到任何東西的,所以必須把findViewById的動作放到setContentView之後

 

所以如果写程序必须先把基础的view添加好setContentView后才能做检索,否则根本不可能使用R.layout.main,感觉google这么干也就是想用layout的main.xml做基础的构架,以我目前的理解,不知道动态动态生成的View是否可以使用R.java进行检索的。

 
3 第三种情况;

*)if you run the Android app, and it displays an exception, then you find the resource returned by findviewbyid is null,

most likely you are using the old style "id" in you xml layout file, change it to "android.id" will fix your problem.

*)if there is a compile error related to R.id.**, please build your project manually to refresh the R file.

if you perform a clean and build, eclipse may indicate you to fix project build path error, yet you just need to remove the R file and recreate it by building the project once again.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多