Android R一直困扰着我很长一段时间。我已经在我的很多项目中看到它,有时它们只是消失了,有时我不得不重新创建一个项目等等。但这对我目前的项目来说并没有解决任何问题。
逻辑上我怀疑XML文件包含错误但我无法找到它。
我使用Eclipse作为我的开发环境,这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/title"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/btnNewTime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/overview" />
<Button
android:id="@+id/btnOverview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/overview" />
<Button
android:id="@+id/btnValues"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/values" />
<Button
android:id="@+id/btnExit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit" />
</LinearLayout>
这是我的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Werkregistratie</string>
<string name="title">Werkregistratie</string>
<string name="overview">Overzicht</string>
<string name="values">Salaris/reiskosten aanpassen</string>
<string name="exit">Afsluiten</string>
</resources>
我无法找到问题所以我真的希望你能帮助我。
答案 0 :(得分:1)
好吧这似乎只是eclipse中的一个错误,相当一些答案是部分正确的。确保名称有效(在某些情况下可以使用Lint)。确保xmls验证。然后检查构建路径是否有错误。之后做一个项目清理,一切都应该没问题。
此外,这个问题并没有出现在新的Android sdk中,所以他们修复了它看起来的东西。
答案 1 :(得分:0)
如果您的包名是“com.example.test”
然后尝试这样的事情 -
findViewById(com.example.test.R.id.textview1);
不喜欢这个 -
findViewById(R.id.textview1);
(*仅在未生成R文件时使用)
答案 2 :(得分:0)
我尝试了你的xml,它的工作对我很好,希望你的java类代码可能有一些问题。尝试找出你内心的问题,并确保清理项目。 一切顺利:)
答案 3 :(得分:0)
如果您的软件包名称为com.foo.bar.package
,那么您需要import
这样:
import com.foo.bar.package.R;
然后findViewById
应该使用这种表示法:
Button btnValue = (Button)findViewById(R.id.btnValue);
有时,当涉及到“缺少资源”时,Eclipse可能会非常讨厌,因为找不到生成的R
文件。
答案 4 :(得分:0)
今天我从工作区导入app src时遇到了这个问题。使用R的所有代码行都显示错误。
我在某处读到了我应该从properties / android视图中将tagetSDK设置为4.0。和minsdk版本在清单中为8。
它就像魅力一样。
另外一件事可能是任何res文件/ layouts / drawable的问题都有大写字母的名字。有些时候Thumbs.db在drawable文件夹中生成并导致同样的问题。
感谢!!!
答案 5 :(得分:0)
我想我以前遇到过这个问题,唯一可以解决的问题就是再次尝试一个新项目并导入我的代码。我知道效率低,笨重,但这是我在经历了几个小时的挫折之后设法让R文件生成的原因。
如果你没有找到可以帮助你的东西here