我在eclipse中使用google-api-java-client構建了一個簡單的Android項目。我試圖按照this tutorial來啓動並運行。我已經通過SO搜索瞭解如何將JAR添加到Eclipse中的Android項目。他們大多建議將JAR添加到Android項目中的lib/
文件夾中,然後將這些JAR添加到項目的構建路徑中。我已經完成了這兩件事。該項目編譯罰款(無論如何,Eclipse不抱怨任何錯誤)。但是,當我在模擬器中運行Android應用程序時,無論何時嘗試在google-api-java-client
JAR中實例化任何類時,都會得到ClassDefNotFoundError
。例如在eclipse中將外部jar添加到android項目中
new com.google.api.client.http.apache.ApacheHttpTransport();
提高ClassDefNotFoundError
。
下面是導致該錯誤代碼:
package com.mycom.android;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
public class SearchRunner implements OnClickListener {
private static final String API_KEY = "AIzaSyA1Mg3xWXfoov4HdPUzYY2NwTxuvCev1-E";
private static final String PLACES_SEARCH_URL = "";
@Override
public void onClick(View v) {
EditText editText = (EditText) v;
String searchText = editText.getText().toString();
runSearch(searchText);
}
protected void runSearch(String searchText) {
new com.google.api.client.http.apache.ApacheHttpTransport();
}
}
這裏是Eclipse的logcat的一個更完整的輸出:
12-21 15:42:11.402: E/dalvikvm(3412): Could not find class 'com.google.api.client.http.apache.ApacheHttpTransport', referenced from method com.mycom.android.SearchRunner.runSearch
12-21 15:42:11.402: W/dalvikvm(3412): VFY: unable to resolve new-instance 70 (Lcom/google/api/client/http/apache/ApacheHttpTransport;) in Lcom/mycom/android/SearchRunner;
12-21 15:42:11.402: D/dalvikvm(3412): VFY: replacing opcode 0x22 at 0x0000
12-21 15:42:11.402: D/dalvikvm(3412): DexOpt: unable to opt direct call 0x00ca at 0x02 in Lcom/mycom/android/SearchRunner;.runSearch
12-21 15:42:11.682: I/MapActivity(3412): Handling network change notification:CONNECTED
12-21 15:42:11.682: E/MapActivity(3412): Couldn't get connection factory client
12-21 15:42:11.791: D/gralloc_goldfish(3412): Emulator without GPU emulation detected.
12-21 15:42:12.532: D/dalvikvm(3412): GC_CONCURRENT freed 102K, 3% free 10520K/10823K, paused 5ms+7ms
12-21 15:42:18.422: D/AndroidRuntime(3412): Shutting down VM
12-21 15:42:18.422: W/dalvikvm(3412): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-21 15:42:18.482: E/AndroidRuntime(3412): FATAL EXCEPTION: main
12-21 15:42:18.482: E/AndroidRuntime(3412): java.lang.NoClassDefFoundError: com.google.api.client.http.apache.ApacheHttpTransport
12-21 15:42:18.482: E/AndroidRuntime(3412): at com.mycom.android.SearchRunner.runSearch(SearchRunner.java:20)
上面的鏈接證明非常有幫助。謝謝! – nathansizemore 2013-04-30 03:45:50