eclipse 如何将 SDK 示例项目复制到工作区中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4928784/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:44:55  来源:igfitidea点击:

How to duplicate an SDK-sample project into workspace?

androideclipseeclipse-plugin

提问by Android Eve

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 notbe affected. The rational behind this goal is explained very well in bullets 1-2-3 in this postby @Neutrino.

我的目标是复制 NotePad 示例项目,这样如果我修改源文件以通过实验学习,原始示例文件将不会受到影响。@Neutrino在这篇文章的第1-2-3 条中很好地解释了这个目标背后的原因。

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

根据@Neutrino 的帖子,我执行了以下步骤:

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

步骤 1:将示例代码复制到一个临时位置(工作区目录之外):

 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:

步骤 2. 从副本导入到工作区:

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

这会导致项目创建,但它带有 21 个错误,但没有任何关于导致它们的原因的线索(请记住,我是 Android 新手,我没有编写 NotePad 程序):

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 copyof this NotePad sample build and run?

我做错了什么,我需要什么来制作和运行这个 NotePad 示例的副本

回答by Android Eve

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:将示例代码复制到一个临时位置(工作区目录之外):

 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:

步骤 2. 从副本导入到工作区:

   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:

现在,该项目像以前一样创建,但不是 21 个错误,我只有 1 个警告:

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?

确实如此:C:\android-sdk-windows 中没有AndroidManifest.xml,但NoteList 目录中有一个。为什么要抱怨这个?为什么它在 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).

总之,问题的根源在于我使用的是 Android 7 示例(而不是 Android 8)并且我继续构建目标 1.5(而不是 2.2)。

All is well now. :)

现在一切都很好。:)

回答by Brian D

Try making sure your import statements are correct:

尝试确保您的导入语句是正确的:

R cannot be resolved - Android error

R 无法解析 - Android 错误

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?

另外,你为什么认为它在看C:\sb\workspace\NotesList\bin\resources.ap_?那不是您的项目存储的地方,是吗?



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 > Importand choose the Existing Projects into Workspaceoption
  3. Choose Select root directoryand browse to the Samples folder until you find the NodePad root directory
  4. Click Openor whatever the relevant "OK" option is
  5. Check the box Copy projects into workspace
  6. Click Finish
  1. 从您的工作区中删除项目,也从磁盘中删除它(或者如果您想保留它,则重命名它以免与您将要导入的项目冲突)
  2. 在 Eclipse 中,File > Import选择Existing Projects into Workspace选项
  3. 选择Select root directory并浏览到 Samples 文件夹,直到找到 NodePad 根目录
  4. 单击Open或任何相关的“确定”选项
  5. 选中该框 Copy projects into workspace
  6. 点击 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

这应该会为您的修补导入一个很好的 API 演示副本,使目录树中的原始源代码免于修改

回答by Nikki

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

右键单击您的 Eclipse 项目,选择属性,转到 Android 并确保选择了正确的 SDK。

回答by Diego

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.
  1. “新的”;
  2. “安卓项目”
  3. 插入一个临时项目名称(不是你想要的最终名称);
  4. 选择您已下载示例的“构建目标”;
  5. 选择“从现有示例创建项目”并选择您想要的代码示例;
  6. 点击“完成”;
  7. 右键单击项目并选择“复制”;
  8. 右键单击项目资源管理器中的空白区域并选择“粘贴”;
  9. 插入您为项目选择的最终名称;
  10. 如果您希望项目位于工作区中的常用路径中,请选择“使用默认位置”。

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

就是这样,项目和代码现在已复制并在您的工作区中工作。