Android的,没有发现进口的jar文件类 [英] Android, class not found from imported jar file

查看:145
本文介绍了Android的,没有发现进口的jar文件类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包括在我的Andr​​oid项目的JAR文件作为<一个解释href="http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project">How我可以在Android项目使用外部JAR文件?。随着由MannyNS和维纳亚克B.在这篇文章中,我得到了错误描述这两种方法找不到类的test.libraryCalc.Calc,这是由图书馆提供的类。下面code说明了这个问题:

I included a jar file in my Android project as explained in How can I use external JARs in an Android project?. With both methods described by MannyNS and Vinayak B. in this post I get the error "Could not find class 'test.libraryCalc.Calc" which is the class provided by the library. The following code illustrates the problem:

通过库提供的示例类:Calc.java

Example class provided via library: Calc.java

package test.libraryCalc;

public class Calc {
    public int add(int a, int b){
        return a + b;
    }
}

LibraryTestActivity.java

LibraryTestActivity.java

package test.library;

import test.libraryCalc.Calc;
import android.app.Activity;
import android.os.Bundle;

public class LibraryTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Calc calc = new Calc();
        int c = calc.add(3, 4);
    }
}

我出口含Calc.java到LibraryTest \库\ calc.jar

I exported the jar file containing Calc.java to LibraryTest\libs\calc.jar

和使用添加JAR文件...在Java按钮构建LibraryTest的路径增加了一个参考吧。

and added a reference to it using the "Add JARs..." button in the Java Build Path of LibraryTest

该图书馆展示了在LibraryTest引用库

The library shows up in the Referenced libraries in LibraryTest

LibraryTest没有构建问题,但在模拟器上运行时,它下面是显示在LogCat中:

LibraryTest has no build problems but when running it on the emulator the following is shown in LogCat:

12-27 14:01:33.965: E/dalvikvm(747): Could not find class 'test.libraryCalc.Calc',               referenced from method test.library.LibraryTestActivity.onCreate
12-27 14:01:33.965: W/dalvikvm(747): VFY: unable to resolve new-instance 13   (Ltest/libraryCalc/Calc;) in Ltest/library/LibraryTestActivity;
12-27 14:01:33.995: D/dalvikvm(747): VFY: replacing opcode 0x22 at 0x0008
12-27 14:01:33.995: D/dalvikvm(747): VFY: dead code 0x000a-0013 in Ltest/library/LibraryTestActivity;.onCreate (Landroid/os/Bundle;)V
12-27 14:01:34.065: D/AndroidRuntime(747): Shutting down VM
12-27 14:01:34.065: W/dalvikvm(747): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-27 14:01:34.075: E/AndroidRuntime(747): FATAL EXCEPTION: main
12-27 14:01:34.075: E/AndroidRuntime(747): java.lang.NoClassDefFoundError: test.libraryCalc.Calc
12-27 14:01:34.075: E/AndroidRuntime(747):  at     test.library.LibraryTestActivity.onCreate(LibraryTestActivity.java:14)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.os.Looper.loop(Looper.java:123)
12-27 14:01:34.075: E/AndroidRuntime(747):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 14:01:34.075: E/AndroidRuntime(747):  at java.lang.reflect.Method.invokeNative(Native Method)
12-27 14:01:34.075: E/AndroidRuntime(747):  at java.lang.reflect.Method.invoke(Method.java:521)
12-27 14:01:34.075: E/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-27 14:01:34.075: E/AndroidRuntime(747):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-27 14:01:34.075: E/AndroidRuntime(747):  at dalvik.system.NativeStart.main(Native Method)
12-27 14:06:34.170: I/Process(747): Sending signal. PID: 747 SIG: 9

有什么需要做的事情得到这个工作?感谢您的所有建议。

What needs to be done to get this working? Thanks for all suggestions.

推荐答案

我觉得现在的问题是,你尝试添加罐子,其中包含的Andr​​oid code。你不能做到这一点。要包括的Andr​​oid code,你应该创建的Andr​​oid库。只需创建一个Android项目,并在项目属性的Andr​​oid段设置,这是库项目。之后,你就可以到该库添加到您的项目。欲了解更多关于Android库,你可以阅读这里

I think that the problem is that you try to add jar that contains Android code. You cannot do this. To include Android code you should create Android library. Simply create an Android project and in the project-properties Android section set that this is library project. After that you'll be able to add this library to your projects. For more about Android libraries you can read here.

更新:我想你code现在。它的工作原理在我的情况。我所做的唯一区别是瓶子我检查导出Java源文件和资源的导出过程。希望这会帮助你。试试吧!

Update: I've tried your code now. It works in my case. The only difference that I've made is during export of Jar I've checked Export Java source files and resources. Hope this will help you. Try it!

这篇关于Android的,没有发现进口的jar文件类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆