我正在尝试通过应用程序将数据从网站获取到我的手机!所以我尝试了使用 AsyncTask 的 jsoup,以避免在主线程上使用网络。好吧,一切都很好,但我的应用程序甚至无法启动。它说的错误是下面的 nullpointerexception 是代码和 logcat 片段
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mt= new MyTask();
mt.execute(URL);
mytext =(TextView)findViewById(R.id.mytext);
}
class MyTask extends AsyncTask<String, Void, String>{
Document doc;
String abouttext;
@Override
protected void onPreExecute(){
super.onPreExecute();
mytext.setText("please wait...");
}
protected String doInBackground(String...params){
String url=params[0];
Document doc;
try{
doc = Jsoup.connect(url).get();
Element about = doc.select("div.box-content").first();
String abouttext = about.text();
}
catch(Exception e){
e.printStackTrace();
}
return abouttext;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
mytext.setText(result);
}
}
这是日志
09-21 18:50:36.746: E/Trace(778): error opening trace file: No such file or directory (2)
09-21 18:50:37.445: D/AndroidRuntime(778): Shutting down VM
09-21 18:50:37.445: W/dalvikvm(778): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-21 18:50:37.506: E/AndroidRuntime(778): FATAL EXCEPTION: main
09-21 18:50:37.506: E/AndroidRuntime(778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.web/com.example.web.MainActivity}: java.lang.NullPointerException
09-21 18:50:37.506: E/AndroidRuntime(778): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-21 18:50:37.506: E/AndroidRuntime(778): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
请有人告诉我做错了什么!谢谢!
我想我会发布整个日志
09-21 19:52:34.164: D/AndroidRuntime(796): Shutting down VM
09-21 19:52:34.194: W/dalvikvm(796): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-21 19:52:34.244: E/AndroidRuntime(796): FATAL EXCEPTION: main
09-21 19:52:34.244: E/AndroidRuntime(796): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.web/com.example.web.MainActivity}: java.lang.NullPointerException
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.os.Looper.loop(Looper.java:137)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-21 19:52:34.244: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 19:52:34.244: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:511)
09-21 19:52:34.244: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-21 19:52:34.244: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-21 19:52:34.244: E/AndroidRuntime(796): at dalvik.system.NativeStart.main(Native Method)
09-21 19:52:34.244: E/AndroidRuntime(796): Caused by: java.lang.NullPointerException
09-21 19:52:34.244: E/AndroidRuntime(796): at com.example.web.MainActivity$MyTask.onPreExecute(MainActivity.java:37)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.os.AsyncTask.execute(AsyncTask.java:534)
09-21 19:52:34.244: E/AndroidRuntime(796): at com.example.web.MainActivity.onCreate(MainActivity.java:27)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.Activity.performCreate(Activity.java:5104)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-21 19:52:34.244: E/AndroidRuntime(796): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-21 19:52:34.244: E/AndroidRuntime(796): ... 11 more