eclipse 如何在android中从另一个项目和包中导入.java

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

How to import .java from another project AND package in android

javaandroideclipseimport

提问by tfrascaroli

We're working on a project (Project A) in college that has to extend from a class that is another project (Project B), made years ago. This is working under Eclipse, and developing apps for android, so this is in fact Activities we're talking about.

我们正在大学开展一个项目(项目 A),该项目必须从多年前制作的另一个项目(项目 B)的班级扩展而来。这是在 Eclipse 下工作,并为 android 开发应用程序,所以这实际上是我们正在谈论的活动。

So far I've tried this:

到目前为止,我已经尝试过这个:

  • Adding project B on Project A's build path (In the Projects TAB)
  • Importing, with full package name and .java name.
  • 在项目 A 的构建路径上添加项目 B(在项目选项卡中)
  • 导入,带有完整的包名和 .java 名称。

The thing is that every solution I've found is for working with .jar, .class or even .apk files, but I'M NOT working with that, project B is a simple project full of .java files.

问题是,我找到的每个解决方案都适用于 .jar、.class 甚至 .apk 文件,但我不使用它,项目 B 是一个充满 .java 文件的简单项目。

To be a little more clear about it, Project A has a class named ClassFromProjectA, and Project B has a class like ClassFromProjectB. ClassFromProjectAis like follows:

更清楚一点的是,项目 A 有一个名为 ClassFromProjectA 的类,项目 B 有一个类似ClassFromProjectB. ClassFromProjectA如下所示:

public class ClassFromProjectA extends ClassFromProjectB{}

ClassFromProjectB is like follows:

ClassFromProjectB 如下所示:

public class ClassFromProjectB extends ListActivity implements OnInitListener, OnGesturePerformedListener {}

The "only" thing ClassFromProjectB is doing is, uppon extending from it, it changes the default layout for a ListView, applies specific navigation options, applies gestures to every activity extending from it, and a couple more tweaks.

ClassFromProjectB 正在做的“唯一”事情是,从它扩展后,它会更改 ListView 的默认布局,应用特定的导航选项,将手势应用于从它扩展的每个活动,以及更多的调整。

If I make this:

如果我这样做:

public class ClassFromProjectA extends ListActivity{}

Everything works fine with my class, no errors, no warnings, no nothing.

我的班级一切正常,没有错误,没有警告,什么也没有。

We are ABSOLUTELY sure that ClassFromProjectBis working, it was a previous succesfull college project, and it also has an Example activity within its files to check functionality, and everything works like a charm. That's the thing though, it only works because it is in the same project file. But we CAN'T do that, we have to import and extend, and it isn't workin'.

我们绝对确定这ClassFromProjectB是有效的,这是以前成功的大学项目,并且它的文件中还有一个示例活动来检查功能,一切都像魅力一样。事情就是这样,它只能工作,因为它在同一个项目文件中。但是我们不能那样做,我们必须导入和扩展,而且它不起作用。

Please, let me know if you know how to make this work, or point me in the right direction.

如果您知道如何进行这项工作,请告诉我,或者为我指明正确的方向。

回答by Leeeeeeelo

What you can do if you have the complete project B that you need to include inside your project (project A).

如果您拥有需要包含在项目(项目 A)中的完整项目 B,您可以做什么。

  1. Make Project B a library in the following manner :
    • Right click the project Bin Eclipse, and click on Properties.
    • Select Androidthen the isLibrarycheck box. Project B Properties
  2. Once Project B is a library, you have to add the library B (formerly project B) in your project A in the following manner :
    • Right click the project Ain Eclipse, and click on Properties.
    • Select Androidthen click on Add.... Add Library
    • You should see a list with all the library projects inside eclipse on your machine. You should also see Project B. Select it and click OK. Library Selection
    • You should see Project B in the list of libraries. Project B added as Library
  1. 按以下方式使项目 B 成为库:
    • 在 Eclipse 中右键单击项目 B,然后单击Properties
    • 选择Android,然后选择isLibrary复选框。 项目 B 属性
  2. 一旦项目 B 是一个库,您必须通过以下方式在项目 A 中添加库 B(以前称为项目 B):
    • 在 Eclipse 中右键单击项目 A,然后单击Properties
    • 选择Android然后点击Add...添加图书馆
    • 您应该会在您的机器上的 eclipse 中看到一个包含所有库项目的列表。您还应该看到Project B。选择它并单击“确定”图书馆选择
    • 您应该会在库列表中看到项目 B。 项目 B 添加为库

Now you should be able to import packages from Project B to project A and use its classes with no problem.

现在您应该能够将包从项目 B 导入到项目 A 并毫无问题地使用它的类。

回答by shhp

Try reference projectB in projectA as follows:

尝试参考 projectA 中的 projectB 如下:

  1. Open "Properties" window of projectA
  2. Go to "Android" Tab
  3. Click "Add" button in the "Library"
  4. Select projectB as reference
  1. 打开projectA的“属性”窗口
  2. 转到“Android”选项卡
  3. 单击“库”中的“添加”按钮
  4. 选择projectB作为参考

After this you can import class from projectB.

在此之后,您可以从 projectB 导入类。