<span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">setContentView</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">(</span><span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">R</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">.</span><span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">layout</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">.</span><span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">activity_main</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">)</span>
提示信息是:
activity_main cannot be resolved or is not a field
我就觉得很奇怪,我在R.layout里面明明看到自动生成了默认的布局activity_main,为什么在这里引用就出错呢。Google之也没发现原因,而且,如果删掉activity_main,Eclipse给出的自动提示是activity_list_item.我当时就觉得可能是导入的某个包覆盖了我真正要引用的R.java.
然后在import里面果然找到了这么一句:
<span class="kwd" style="color:#0088;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">import</span><span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px"> android</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">.</span><span class="pln" style="PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">R</span><span class="pun" style="color:#66660;PADDING-BOTTOM: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px">;</span>
删掉这一句就正常了。
出现这个问题的原因是,如果你在R.java还没有来得及自动生成的时候选择了fix imports,那么Eclipse就会自动帮你导入android.R这个包,就会覆盖掉本地的R.java,所以就导致了你不能正常引用R.layout.activity_main.