10

My goal is to duplicate the NotePad sample project such that if I tinker with the source files in order to learn through experimentation, the original sample files will not be affected. The rational behind this goal is explained very well in bullets 1-2-3 in this post by @Neutrino.

Based on @Neutrino's post, I performed the following steps:

Step 1: Copy sample code to a temporary location (outside of the workspace directory):

 1. Close Eclipse.
 2. Copy the entire sample folder
    **NotePad** from C:\android-sdk-windows\samples\android-7
    to C:\Users\androideve\Documents\NotePad

Step 2. Import from copy to workspace:

   1. Start Eclipse
   2. File > New > Project > Android Project [Next]
      > Create project from existing **source**: C:\Users\androideve\Documents\NotePad
   3. Delete project from Package Explorer (without deleting its files from disk!)
   4. File > Import... 
      > General > Existing Projects into Workspace [Next]
        > Browse: C:\Users\androideve\Documents\NotePad
        > Check "Copy projects into workspace" [Finish]
   5. Delete entire folder C:\Users\androideve\Documents\NotePad from disk.

This results in the project creation but it comes with 21 errors without any clue as to what causes them (remember, I am an Android newbie and I didn't write the NotePad program):

Description Resource    Path    Location    Type
Error generating final archive: java.io.FileNotFoundException: C:\sb\workspace\NotesList\bin\resources.ap_ does not exist   NotesList       Unknown Android Packaging Problem
error: Error: String types not allowed (at 'layout_height' with value 'match_parent').  note_editor.xml /NotesList/res/layout   line 17 Android AAPT Problem
error: Error: String types not allowed (at 'layout_width' with value 'match_parent').   note_editor.xml /NotesList/res/layout   line 17 Android AAPT Problem
error: Error: String types not allowed (at 'layout_width' with value 'match_parent').   noteslist_item.xml  /NotesList/res/layout   line 17 Android AAPT Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 148    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 151    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 175    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 177    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 194    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 195    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 265    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 269    Java Problem
R cannot be resolved to a variable  NoteEditor.java /NotesList/src/com/example/android/notepad  line 276    Java Problem
R cannot be resolved to a variable  NotesList.java  /NotesList/src/com/example/android/notepad  line 83 Java Problem
R cannot be resolved to a variable  NotesList.java  /NotesList/src/com/example/android/notepad  line 94 Java Problem
R cannot be resolved to a variable  NotesList.java  /NotesList/src/com/example/android/notepad  line 177    Java Problem
R cannot be resolved to a variable  NotesLiveFolder.java    /NotesList/src/com/example/android/notepad  line 48 Java Problem
R cannot be resolved to a variable  NotesLiveFolder.java    /NotesList/src/com/example/android/notepad  line 51 Java Problem
R cannot be resolved to a variable  TitleEditor.java    /NotesList/src/com/example/android/notepad  line 71 Java Problem
R cannot be resolved to a variable  TitleEditor.java    /NotesList/src/com/example/android/notepad  line 80 Java Problem
R cannot be resolved to a variable  TitleEditor.java    /NotesList/src/com/example/android/notepad  line 83 Java Problem

What did I do wrong and what do I need to make the copy of this NotePad sample build and run?

Community
  • 1
  • 1
Android Eve
  • 14,864
  • 26
  • 71
  • 96

4 Answers4

11

OK, I think I am getting there (note the subtle differences between the original steps and the following ones):

Step 1: Copy sample code to a temporary location (outside of the workspace directory):

 1. Close Eclipse.
 2. Copy the entire sample folder
    **NotePad** from C:\android-sdk-windows\samples\android-8
    to C:\Users\androideve\Documents\NotePad

Step 2. Import from copy to workspace:

   1. Start Eclipse
   2. File > New > Project > Android Project [Next]
      > Create project from existing **source**: C:\Users\androideve\Documents\NotePad
      > Build Target: **Uncheck** Android 1.5. **Check** Android 2.2. [Finish]
   3. Delete project from Package Explorer (without deleting its files from disk!)
   4. File > Import... 
      > General > Existing Projects into Workspace [Next]
        > Browse: C:\Users\androideve\Documents\NotePad
        > Check "Copy projects into workspace" [Finish]
   5. Delete entire folder C:\Users\androideve\Documents\NotePad from disk.

Now, the project is created as before but instead of 21 errors, I have only 1 warning:

Description Resource    Path    Location    Type
Attribute minSdkVersion (3) is lower than the project target API level (8)  AndroidManifest.xml /NotesList  line 1  Android ADT Problem

I also have two red messages on the Console:

[2011-02-10 12:45:50 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Unable to read C:\android-sdk-windows\AndroidManifest.xml: java.io.FileNotFoundException: C:\android-sdk-windows\AndroidManifest.xml (The system cannot find the file specified)
[2011-02-10 12:45:50 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Unable to read C:\android-sdk-windows\AndroidManifest.xml: java.io.FileNotFoundException: C:\android-sdk-windows\AndroidManifest.xml (The system cannot find the file specified)

It's true: There is no AndroidManifest.xml in C:\android-sdk-windows, but there is one in the NoteList directory. Why is it complaining about this? Why is it looking in C:\android-sdk-windows?

Anyway, after creating a launch configuration, the copied-verbatim sample project proceeded to run on the emulator without any problems (now I can finally proceed to learning the programming side of it).

In summary, the source of the problem was that I was using the Android 7 sample (instead of Android 8) and that I proceeded with build target 1.5 (instead of 2.2).

All is well now. :)

Android Eve
  • 14,864
  • 26
  • 71
  • 96
2

Try making sure your import statements are correct:

R cannot be resolved - Android error

Also, why do you think it's looking at C:\sb\workspace\NotesList\bin\resources.ap_? That's not where your project is stored, is it?


Might I suggest trying to do this instead:

  1. Delete the project from your workspace, also delete it from the disk (or rename it if you want to keep it so it doesn't conflict with the project you are about to import)
  2. In Eclipse, File > Import and choose the Existing Projects into Workspace option
  3. Choose Select root directory and browse to the Samples folder until you find the NodePad root directory
  4. Click Open or whatever the relevant "OK" option is
  5. Check the box Copy projects into workspace
  6. Click Finish

This should import a nice carbon-copy of the API demo for your tinkering, leaving the original source in the directory tree free from modifications

Community
  • 1
  • 1
Brian D
  • 9,863
  • 18
  • 61
  • 96
  • Wow! I didn't realize the Android development tools are so inconsistent (sometimes Ctrl+Shift+O solves a problem, sometimes it would create a problem). Anyway, I tried both -- that didn't help. As for your question: Yes, C:\sb\workspace is where my project is stored -- note that it's **copied** from the sample. And +1 for the 1st helpful attempt. – Android Eve Feb 10 '11 at 04:55
  • I just discovered that NotesList>res>layout doesn't have any main.xml file. Could this be the source of the problem? If so, how could this be? Shouldn't a sample project (that comes with the Android SDK) be exemplary? – Android Eve Feb 10 '11 at 04:59
  • @androideve It doesn't need a main.xml if it's never called in the activity. There are others: node_editor.xml, noteslist_item.xml, and title_editor.xml – Brian D Feb 10 '11 at 05:09
  • @androideve I've added another way to import a demo project... this might work for you. I just tried it and it was flawless. – Brian D Feb 10 '11 at 05:21
  • This is unreal. I followed your instructions **to the letter**. Yet, Eclipse refuses to display any project in the **Projects** box, after I select the found directory. I tried C:\android-sdk-windows\samples\android-9\NotePad, C:\android-sdk-windows\samples\android-8\NotePad, C:\android-sdk-windows\samples\android-7\NotePad, C:\android-sdk-windows\samples\android-7 and C:\android-sdk-windows\samples -- to no avail. What's going on? (also note that your method didn't work for @Neutrino either). – Android Eve Feb 10 '11 at 17:13
2

Right click on your Eclipse project, choose properties, go down to Android and make sure the correct SDK is selected.

Nikki
  • 261
  • 1
  • 4
  • 10
1

Simpler solution:

  1. "New";
  2. "Android Project"
  3. insert a temporary project name (not the final you want);
  4. select a "Build Target" for which you have downloaded examples;
  5. select "Create project from existing sample" and choose the code sample you want;
  6. click "Finish";
  7. right click on the project and select "Copy";
  8. right click on a empty area in the Project Explorer and select "Paste";
  9. insert the final name you've choosen for the project;
  10. select "Use default location" if you want the project to be in the usual path within the workspace.

That's it, the project and the code is now duplicated and working in your workspace.

Diego
  • 5,326
  • 1
  • 35
  • 32