3

我正在通过 android 开发网站上的记事本练习,目前正在进行练习(在此处找到)。

我来到教程的最后,发现我在主要的两个java文件中关于R文件关系有几个错误,例如setContentView(R.layout.notes_list);menu.add(0, INSERT_ID,0, R.string.menu_insert);

我试过清洁和再生R.java无济于事。

这些也出现在练习的解决方案中,所以我认为它们是故意的错误,或者是我的日食,但我似乎无法修复它们,任何帮助都会非常感谢!

4

5 回答 5

7

您的布局文件夹中有布局 notes_list.xml 吗?您的 strings.xml 中是否有一个名为 menu_insert 的字符串?这可能是问题所在,因为我认为他们没有在教程中提供所有内容......他们确实在解决方案中提供了它们:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_notes"/>
</LinearLayout>

在 values 文件夹中的 strings.xml 中:

<string name="menu_insert">Add Note</string>
于 2010-07-23T17:01:00.480 回答
5

I think you might have to be careful about how you use the ctrl-shift-O command (to auto-insert the "import" statements at the top of the file). It could be that you get the "import android.R;" statement as a "bonus". I commented-out the "import android.R;" statement, and everything was fine.

于 2010-11-25T22:14:58.603 回答
3

是的,主要问题是 Eclipse 导入 android.R,当 ctrl+shift+o 按下时。解决问题的另一种方法是使用完整路径声明导入:

import com.android.demo.notepad2.R;
于 2011-03-24T07:50:54.223 回答
0

您应该检查是否已在您使用的任何布局 xml 文件中向 LinearLayout 添加了一个 id。如果是这种情况,那么您应该寻找会阻止 R 文件被重建的语法错误。

就像是:

android:id="@+id/linearLayout"
于 2012-12-11T18:52:28.093 回答
0

硬编码字符串时,不要在两个.xml文件中都使用空格。即@string/a b c是错误的尝试@string/a_b_c

于 2013-01-02T12:59:02.640 回答