Cannot resolve R after adding new layout XML files
I had problems with resolving R several times after I added new layout XML files. I use Eclipse and API 8. Sometimes it goes away after I "clean" the project, or close and then reopen the project. I tried to add "import android.R" but it didn't help. Someone said that this is because some layout XML files have errors, but the file looks right to me.
Here is an XML file, res/layout/status.xml
, that I had problems with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="Button" android:id="@+id/button_tmp" android:layout_height="wrap_content" android:layout_width=开发者_如何学运维"wrap_content"></Button>
</LinearLayout>
and here is how I use it:
public class StatusActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.status);
}
}
One more thing, this XML file has a question mark in the "Package Explore" panel in Eclipse, but the main.xml
has a different icon. I have only two layout XML files now.
Do I need to do something special in Eclipse to add a new layout XML file?
Thanks for your help!
Update a screenshot of icons in the "Package Explore" panel:
Finally figured it out. There is nothing wrong with the layout XML file. The problem is that the StatusActivity
class is not in the top-level package, but the R class auto-generated is in the top-level package. So in Status.java
, I need to import it, but in the main activity class, which is in the top-level package, I don't. That's why main.xml
works but status.xml
does not.
As for the different icon, it indicate if this file is managed by the SCM system. Since status.xml
is new, so it has a question mark.
Click over the in the package explorer and try the F5 refresh key - forget who first suggested that but it beats restarting eclipse!
Try to changed below in LinearLayout.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
Are you Test Project > Clean
at this time?
some times you should clean project to rebuild R
file.
精彩评论