无法解析或不是字段(来自 android dev 网站的记事本练习)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3320466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot be resolved or is not a field (Notepad exercises from android dev website)
提问by Valin
I'm running through the notepad exercises on the android dev website, currently working on exercise (found here).
我正在 android 开发网站上运行记事本练习,目前正在练习(在这里找到)。
I came to the end of the tutorial and found that I had several errors in the main two java files regarding R file relations, such as setContentView(R.layout.notes_list);
and menu.add(0, INSERT_ID,0, R.string.menu_insert);
来到教程的最后,发现主要的两个java文件中关于R文件关系的几个错误,比如setContentView(R.layout.notes_list);
和menu.add(0, INSERT_ID,0, R.string.menu_insert);
I've tried cleaning and re-genning R.java
to no avail.
我试过清理和重新生成R.java
无济于事。
These also occur in the solution for the exercise so I think they are deliberate mistakes, or something with my eclipse, but I can't seem to fix them, any help would be great thanks!
这些也出现在练习的解决方案中,所以我认为它们是故意的错误,或者我的日食有什么问题,但我似乎无法修复它们,任何帮助都会非常感谢!
回答by Sephy
Do you have the layout notes_list.xml in your layout folder? Do you have a string by the name of menu_insert in your strings.xml? This might be the issue, because I think they don't give EVERYTHING in the tutorial... They do have them in the solution :
您的布局文件夹中有布局 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>
in strings.xml in values folder :
在 values 文件夹中的 strings.xml 中:
<string name="menu_insert">Add Note</string>
回答by Andrew Jens
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.
我认为您可能必须小心如何使用 ctrl-shift-O 命令(在文件顶部自动插入“导入”语句)。可能是你得到了“import android.R;” 声明为“奖金”。我注释掉了“import android.R;” 声明,一切都很好。
回答by Valin
Yeah, main problem is that Eclipse importing android.R, when ctrl+shift+o pressed. Another way to fix problem is declaring import with full path:
是的,主要问题是 Eclipse 导入 android.R,当按下 ctrl+shift+o 时。解决问题的另一种方法是使用完整路径声明导入:
import com.android.demo.notepad2.R;
回答by Euler Geek
You should check to see that you've added an id to your LinearLayout in whatever layout xml file you're using. If that's the case, then you should look for something syntactically wrong that would prevent the R file from being rebuilt.
您应该检查是否在您使用的任何布局 xml 文件中向 LinearLayout 添加了 id。如果是这种情况,那么您应该寻找一些语法错误,以防止重新构建 R 文件。
Something like:
就像是:
android:id="@+id/linearLayout"
回答by shon
While hardcoding string, don't use space in both .xml
files. i.e @string/a b c
is wrong try @string/a_b_c
在对字符串进行硬编码时,不要在两个.xml
文件中都使用空格。即@string/a b c
错误尝试@string/a_b_c