我对android很新,我正在尝试使用Jsoup从网站检索一些数据。 这是我在Java项目中使用的确切代码,它起作用。但是当我将它转移到Android并将“println”更改为“setText()”时,应用程序“意外停止”。
package com.ex.myschoolapp;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Document doc = null;
try {
doc = Jsoup.connect("http://moisilonesti.licee.edu.ro/concursuri.php").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements sElem = doc.select("div#olimpiade table tbody tr th");
String str = sElem.text();
TextView stv = (TextView) findViewById(R.id.textView2);
stv.setText(str);}}
这是LogCat跟踪:
11-17 17:37:59.145: E/AndroidRuntime(23601): FATAL EXCEPTION: main
11-17 17:37:59.145: E/AndroidRuntime(23601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.myschoolapp/com.ex.myschoolapp.MainActivity}: java.lang.NullPointerException
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.os.Looper.loop(Looper.java:123)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread.main(ActivityThread.java:3701)
11-17 17:37:59.145: E/AndroidRuntime(23601): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 17:37:59.145: E/AndroidRuntime(23601): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 17:37:59.145: E/AndroidRuntime(23601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
11-17 17:37:59.145: E/AndroidRuntime(23601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
11-17 17:37:59.145: E/AndroidRuntime(23601): at dalvik.system.NativeStart.main(Native Method)
11-17 17:37:59.145: E/AndroidRuntime(23601): Caused by: java.lang.NullPointerException
11-17 17:37:59.145: E/AndroidRuntime(23601): at com.ex.myschoolapp.MainActivity.onCreate(MainActivity.java:67)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 17:37:59.145: E/AndroidRuntime(23601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-17 17:37:59.145: E/AndroidRuntime(23601): ... 11 more
我不知道如何使用AsyncTask但是从我在Jsoup食谱中看到的内容中我不需要任何繁琐的工作。
答案 0 :(得分:0)
试试这段代码:
Document doc = Jsoup.connect("http://example.com/").get();
Elements divs = doc.select("div#test");
for(Element div : divs)
System.out.println(div.text());
答案 1 :(得分:0)
您的代码适合我,我可以在TextView中打印数据。
你有NullPointer,因为你没有正确设置setContentView,因为你的findViewById返回null ..,
setContentView(R.layout.your_activity_xml);