开发者

Cannot be resolved or is not a field (Notepad exercises from android dev website)

开发者 https://www.devze.com 2023-01-08 09:21 出处:网络
I\'m running through the notepad exercises on the android dev website, currently working on exercise (found here).

I'm running through the notepad exercises on the android dev website, currently working on exercise (found here).

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);

I've tried c开发者_StackOverflow社区leaning and re-genning R.java to no avail.

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!


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 :

<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 :

<string name="menu_insert">Add Note</string>


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.


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:

import com.android.demo.notepad2.R;


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.

Something like:

android:id="@+id/linearLayout"


While hardcoding string, don't use space in both .xml files. i.e @string/a b c is wrong try @string/a_b_c

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号