2012-07-22 112 views
0

我試圖在Android中測試此api:http://code.google.com/p/google-api-spelling-java/。我創建了一個新的應用程序,並將庫和依賴關係從prev url複製到項目的「libs」目錄,然後通過Java Build Path-> Libraries-> Add jars添加對它們的引用...Android外部庫引用和NoClassDefFoundError

以下是測試活動:

package com.jimmcgowen.googleapispelling; 

import org.xeustechnologies.googleapi.spelling.SpellChecker; 
import org.xeustechnologies.googleapi.spelling.SpellCorrection; 
import org.xeustechnologies.googleapi.spelling.SpellResponse; 

import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Log.i("classpath", System.getProperty("java.class.path")); 
    } 

    public void onDoIt(View view) { 
     EditText input = (EditText)findViewById(R.id.PlainText); 
     String word = input.getText().toString().trim(); 

     SpellChecker checker = new SpellChecker(); 
     SpellResponse spellResponse = checker.check(word); 

     for (SpellCorrection sc : spellResponse.getCorrections()) { 
      Log.i("Spell Checker", sc.getValue()); 

      TextView tv = new TextView(this); 
      tv.setText(sc.getValue()); 

      LinearLayout container = (LinearLayout)findViewById(R.id.ResultContainer); 
      container.addView(tv); 
     } 
    } 
} 

哪個編譯罰款,但是當我運行它,我得到:

07-22 16:16:48.673: E/dalvikvm(29209): Could not find class 'org.xeustechnologies.googleapi.spelling.SpellChecker', referenced from method com.jimmcgowen.googleapispelling.MainActivity.onDoIt 
07-22 16:16:48.703: W/dalvikvm(29209): VFY: unable to resolve new-instance 28 (Lorg/xeustechnologies/googleapi/spelling/SpellChecker;) in Lcom/jimmcgowen/googleapispelling/MainActivity; 
07-22 16:16:48.713: D/dalvikvm(29209): VFY: replacing opcode 0x22 at 0x0015 
07-22 16:16:48.713: D/dalvikvm(29209): VFY: dead code 0x0017-004c in Lcom/jimmcgowen/googleapispelling/MainActivity;.onDoIt (Landroid/view/View;)V 
07-22 16:16:48.903: I/classpath(29209): . 
07-22 16:16:48.903: D/ATRecorder(29209): com.htc.autotest.dlib.RecordEngine in loader [email protected] 
07-22 16:16:53.948: D/AndroidRuntime(29209): Shutting down VM 
07-22 16:16:53.958: W/dalvikvm(29209): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0) 
07-22 16:16:53.978: E/AndroidRuntime(29209): FATAL EXCEPTION: main 
07-22 16:16:53.978: E/AndroidRuntime(29209): java.lang.IllegalStateException: Could not execute method of the activity 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.view.View$1.onClick(View.java:2192) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.view.View.performClick(View.java:2533) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.view.View$PerformClick.run(View.java:9299) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.os.Handler.handleCallback(Handler.java:587) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.os.Looper.loop(Looper.java:150) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.app.ActivityThread.main(ActivityThread.java:4352) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at java.lang.reflect.Method.invokeNative(Native Method) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at java.lang.reflect.Method.invoke(Method.java:507) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at dalvik.system.NativeStart.main(Native Method) 
07-22 16:16:53.978: E/AndroidRuntime(29209): Caused by: java.lang.reflect.InvocationTargetException 
07-22 16:16:53.978: E/AndroidRuntime(29209): at java.lang.reflect.Method.invokeNative(Native Method) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at java.lang.reflect.Method.invoke(Method.java:507) 
07-22 16:16:53.978: E/AndroidRuntime(29209): at android.view.View$1.onClick(View.java:2187) 
07-22 16:16:53.978: E/AndroidRuntime(29209): ... 11 more 
07-22 16:16:53.978: E/AndroidRuntime(29209): Caused by: java.lang.NoClassDefFoundError: org.xeustechnologies.googleapi.spelling.SpellChecker 
07-22 16:16:53.978: E/AndroidRuntime(29209): at com.jimmcgowen.googleapispelling.MainActivity.onDoIt(MainActivity.java:29) 
07-22 16:16:53.978: E/AndroidRuntime(29209): ... 14 more 

注意的classpath在運行時爲空。我認爲這是問題所在,但我不確定該從哪裏出發。

使用API​​級別8 SDK工具20 平臺工具13 我剛剛更新了eclipe插件。

回答

0

將jar添加到資產文件夾,然後將它們鏈接到項目構建路徑。不要忘記在訂單和導出標籤中選擇罐子。