Android的findViewById是返回NULL [英] Android findViewById is returning NULL

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

问题描述

有时我有一个奇怪的问题,我的XML视图,并包含在Android的是Eclipse SDK的子元素。

sometime I have a strange problem with my xml views and contained sub elements in Android Eclipse SDK.

例如,我有一个名为main.xml中带有的LinearLayout和一个TextView作为唯一的孩子一个XML视图,用id的 textView1 的。一切为(长)时工作正常。在做一些重大code的变化,它的可能发生,那 findViewById(R.id.textView1); 开始返回的的。重新编译不会改变任何东西。这是奇怪的。

For example, I have a xml view called main.xml with a LinearLayout and a TextView as only child, with the id textView1. Everything works fine for a (long) time. When doing some major code changes, it might happen, that findViewById(R.id.textView1); starts returning null. Recompilation does not change anything. That is strange.

一段时间后我发现了一个解决方法......我复制的的main.xml 的到的 main2.xml 的,改变的setContentView(R.layout。主)的setContentView(R.layout.main2)和一切工作正常ag​​aing,在不改变以外的任何其他布局的参考,甚至布局本身只是复制。没有内容被修改。的第一个工作日运行后,我可以删除的 main2.xml 的设置和布局回 R.layout.main 。没有更多的NullPointerException。

After some time I found a workaround... I copied main.xml to main2.xml and changed setContentView(R.layout.main) to setContentView(R.layout.main2) and everything was working fine againg, without changing anything other than the layout reference, even the layout itself was only copied. No content was modified. After the first working run, I can delete main2.xml and set the layout back to R.layout.main. No more NullPointerException.

是否有人有一个想法,什么错误,以及如何没有做愚蠢的事情副本来解决这个问题?

Does someone have an idea what is going wrong and how to fix this without doing the stupid copy thing?

最佳,
Hacksteak

Best, Hacksteak

推荐答案

这发生在我身上配发,如果有问题与Eclipse,像强制关闭。什么工作对我来说是清洁项目,直到它得到的所有资源和参考右后卫。

It happens to me allot if there are problems with Eclipse, like a force close. What it works for me is cleaning the project until it get all resources and references right back.

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

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