2

我无法将库(在我的情况下为 ksoap)嵌入到我的项目中。运行时出现错误“找不到从方法...引用的类 'org.ksoap2.serialization.SoapObject'”。当然,这个类在 ksoap 库中可用,不用说我的代码中有相应的“import ...”行。我已经尝试以不同的方式嵌入它,我在 stackoverflow 和其他地方找到了这些方式。我通过属性 => 构建路径 => 添加外部 jar 并通过导入 jar、属性 => 构建路径 => 添加 jar 进行了尝试。它没有什么区别,其他库也一样。我还尝试选择 Google API 或 Android 作为构建目标 - 但是,它没有改变。

我正在使用 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)
4

1 回答 1

0

(在问题编辑中回答。转换为社区 wiki 答案。请参阅将问题的答案添加到问题本身时的适当操作是什么?

OP写道:

由于有关 libs 文件夹的提示,解决了该问题。

感谢我收到的另一个提示,我能够找出问题所在。仅将库添加到 libs 文件夹是不够的 - 它还需要从该位置添加到构建路径(属性 => Java 构建路径 => 添加 Jars)。

对于那些熟悉 Java/Android 开发的人来说,这可能是非常明显的——但我对这些东西完全陌生。谢谢您的帮助!

于 2015-02-07T20:32:12.450 回答