我在我的Android項目中包含了一個jar文件,如How can I use external JARs in an Android project?中所述。在這篇文章中,MannyNS和Vinayak B描述了這兩種方法,我得到錯誤「找不到類」test.libraryCalc.Calc「,這是圖書館提供的類。下面的代碼說明了此問題:Android,從導入的jar文件中找不到類
實施例經由庫提供類:Calc.java
package test.libraryCalc;
public class Calc {
public int add(int a, int b){
return a + b;
}
}
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 \庫jar文件\ calc.jar
並在LibraryTest
Java構建路徑中添加對它的引用使用「添加JAR文件...」按鈕,該圖書館展示了在引用的庫中LibraryTest
LibraryTest沒有構建問題,但在模擬器上運行時,它的下面是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
需要採取哪些措施來得到這個工作?感謝您的所有建議。
如果您認爲圖片有幫助,並且您還沒有權限,請隨意將它們上傳到簡單的外部圖像提示,然後添加鏈接。通常情況下,具有編輯和圖片權限的用戶將會停下來並嵌入他們,如果他認爲這些是合適的話。編輯:好吧upvoted超過10.現在你可以張貼他們自己。 :) – 2011-12-27 14:02:36
還要確保您添加的jar是使用Java 6或更早版本編譯的。本帖子發佈時,Android不支持Java 7(2012年7月) – 2012-07-26 01:00:27