我无法将库(ksoap在我的案例中)嵌入到我的项目中。在运行时出现错误“无法找到类”org.ksoap2.serialization.SoapObject'从方法引用...“。当然,这个类在ksoap库中是可用的,不需要提到我的代码中有各自的“import ...”行。我试图用我在这里找到的不同方式将它嵌入到stackoverflow和其他地方。我通过Properties => Build Path =>添加外部jar,并通过导入jar,Properties => Build path =>添加jar。它没有区别,其他库也是如此。我也尝试选择Google API或Android作为Build Target - 但是,它不会改变。Android/Eclipse:如何嵌入.jar文件?
我正在使用Eclipse Juno,该项目是Android 2.2之一。基本上,我一直在关注指南http://www.youtube.com/watch?v=v9EowBVgwSo
请参阅下面的代码和错误消息的完整列表。
package com.example.mytest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.widget.TextView;
public class FirstScreen extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
tv = (TextView)findViewById(R.id.textView01);
tv.setText("Mein Test");
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Celsius", "32");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE aht = new HttpTransportSE(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
}
catch(Exception e)
{
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_first_screen, menu);
return true;
}
}
错误消息:
10-13 12:59:00.734: E/dalvikvm(6567): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.mytest.FirstScreen.onCreate
10-13 12:59:00.734: W/dalvikvm(6567): VFY: unable to resolve new-instance 501 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/mytest/FirstScreen;
10-13 12:59:00.734: D/dalvikvm(6567): VFY: replacing opcode 0x22 at 0x0012
10-13 12:59:00.734: D/dalvikvm(6567): VFY: dead code 0x0014-005a in Lcom/example/mytest/FirstScreen;.onCreate (Landroid/os/Bundle;)V
10-13 12:59:00.875: D/AndroidRuntime(6567): Shutting down VM
10-13 12:59:00.875: W/dalvikvm(6567): threadid=1: thread exiting with uncaught exception (group=0x40015578)
10-13 12:59:00.875: E/AndroidRuntime(6567): FATAL EXCEPTION: main
10-13 12:59:00.875: E/AndroidRuntime(6567): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
10-13 12:59:00.875: E/AndroidRuntime(6567): at com.example.mytest.FirstScreen.onCreate(FirstScreen.java:25)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.os.Looper.loop(Looper.java:130)
10-13 12:59:00.875: E/AndroidRuntime(6567): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 12:59:00.875: E/AndroidRuntime(6567): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:59:00.875: E/AndroidRuntime(6567): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 12:59:00.875: E/AndroidRuntime(6567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 12:59:00.875: E/AndroidRuntime(6567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 12:59:00.875: E/AndroidRuntime(6567): at dalvik.system.NativeStart.main(Native Method)
10-13 12:59:00.878: I/Process(6567): Sending signal. PID: 6567 SIG: 9
10-13 13:03:01.222: E/dalvikvm(6870): Could not find class 'org.ksoap2.serialization.SoapObject', referenced from method com.example.mytest.FirstScreen.onCreate
10-13 13:03:01.222: W/dalvikvm(6870): VFY: unable to resolve new-instance 33 (Lorg/ksoap2/serialization/SoapObject;) in Lcom/example/mytest/FirstScreen;
10-13 13:03:01.222: D/dalvikvm(6870): VFY: replacing opcode 0x22 at 0x0012
10-13 13:03:01.222: D/dalvikvm(6870): VFY: dead code 0x0014-005a in Lcom/example/mytest/FirstScreen;.onCreate (Landroid/os/Bundle;)V
10-13 13:03:01.472: D/AndroidRuntime(6870): Shutting down VM
10-13 13:03:01.472: W/dalvikvm(6870): threadid=1: thread exiting with uncaught exception (group=0x40015578)
10-13 13:03:01.519: E/AndroidRuntime(6870): FATAL EXCEPTION: main
10-13 13:03:01.519: E/AndroidRuntime(6870): java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
10-13 13:03:01.519: E/AndroidRuntime(6870): at com.example.mytest.FirstScreen.onCreate(FirstScreen.java:25)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.os.Looper.loop(Looper.java:130)
10-13 13:03:01.519: E/AndroidRuntime(6870): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-13 13:03:01.519: E/AndroidRuntime(6870): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 13:03:01.519: E/AndroidRuntime(6870): at java.lang.reflect.Method.invoke(Method.java:507)
10-13 13:03:01.519: E/AndroidRuntime(6870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-13 13:03:01.519: E/AndroidRuntime(6870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-13 13:03:01.519: E/AndroidRuntime(6870): at dalvik.system.NativeStart.main(Native Method)
您是否试过简单地在'libs'文件夹中复制jar文件?如果文件夹不存在,只需创建它。 – WarrenFaith
我想你已经足够了,可以将它添加到libs文件夹中。 –
感谢堆,这还不够,但它是正确的方法,请参阅我的要求下面的答案。 – user1743224