我正在使用 Jsoup,在 Android 中,我想从这个网站的第一个标签“img”中获取属性“src”:http: //www.proyectoglass.com/feeds/posts/default? alt=rss ,由于某种原因,我无法确定程序正在停止,而且我一无所知。这是代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView test=(TextView) findViewById(R.id.testText);
try {
Document doc = Jsoup.connect("http://www.proyectoglass.com/feeds/posts/default?alt=rss").ignoreContentType(true).get();
Element img = doc.select("img").first();
test.setText(""+img.attr("abs:src"));
} catch (IOException e) {
test.setText(e.toString());
}
}
这是我的 LogCat:
06-14 15:57:48.064: W/dalvikvm(542): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-14 15:57:48.106: E/AndroidRuntime(542): FATAL EXCEPTION: main
06-14 15:57:48.106: E/AndroidRuntime(542): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsoup_test/com.example.jsoup_test.MainActivity}: java.lang.NullPointerException
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.os.Looper.loop(Looper.java:123)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-14 15:57:48.106: E/AndroidRuntime(542): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 15:57:48.106: E/AndroidRuntime(542): at java.lang.reflect.Method.invoke(Method.java:521)
06-14 15:57:48.106: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-14 15:57:48.106: E/AndroidRuntime(542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-14 15:57:48.106: E/AndroidRuntime(542): at dalvik.system.NativeStart.main(Native Method)
06-14 15:57:48.106: E/AndroidRuntime(542): Caused by: java.lang.NullPointerException
06-14 15:57:48.106: E/AndroidRuntime(542): at com.example.jsoup_test.MainActivity.onCreate(MainActivity.java:28)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 15:57:48.106: E/AndroidRuntime(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-14 15:57:48.106: E/AndroidRuntime(542): ... 11 more
在网络中,我想从 src 获取 url。
<img width="640" height="344" border="0" src="http://4.bp.blogspot.com/-tNEIyl1tTJA/UbRVa2bKpiI/AAAAAAAAAs4/xqW9VPw7b9g/s640/Larry-Page-Google-Project-Glass.jpg"/>