3

该代码在 java 控制台中运行良好,但在 android 模拟器中时会出错。我检查了其他相同类型的帖子,但没有成功...

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Geocoder;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AndroidGPSTrackingActivity extends Activity {
//some code which calls directions

    }



    public void directions() {
        try {

                        Document doc = Jsoup.connect("http://10.0.2.2/kj/jhjh.htm")
                    .get();////////////////error occurs here
            Elements el = doc.getElementsByClass("lkklk");
            String str = el.text();
            str = str.replaceAll("[0-9]+[.] ", "\n");
            string = str.split("\n");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

以下是错误......

01-19 05:34:35.723: E/AndroidRuntime(952): FATAL EXCEPTION: main
01-19 
05:34:35.723: E/AndroidRuntime(952): java.lang.NoClassDefFoundError: org.jsoup.Jsoup
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.example.gpstracking.AndroidGPSTrackingActivity.directions(AndroidGPSTrackingActivity.java:308)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.example.gpstracking.AndroidGPSTrackingActivity.onActivityResult(AndroidGPSTrackingActivity.java:193)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)

01-19 05:34:35.723: E/AndroidRuntime(952):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread.access$1100(ActivityThread.java:130)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at android.os.Looper.loop(Looper.java:137)

01-19 05:34:35.723: E/AndroidRuntime(952):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at java.lang.reflect.Method.invoke(Method.java:511)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-19 
05:34:35.723: E/AndroidRuntime(952):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

你得到一个NoClassDefFoundError因为你的 jar 文件在运行时不可用。为了让它在运行时可用,您必须将它放在您的 libs 文件夹中,并检查您的 java 构建路径中 jar 文件上的复选框,如下所示:

在此处输入图像描述

于 2013-01-19T00:47:50.940 回答