Android应用程序在使用jsoup和AsyncTask时崩溃了!(实际上甚至没有启动)

时间:2013-09-21 19:03:08

标签: android android-asynctask

我试图通过应用从网站获取数据到我的手机! 所以我尝试使用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

2 个答案:

答案 0 :(得分:0)

首先,您在范围内声明了两次Document doc,

我认为你的回归是关于文本的;提出问题

假设服务器或服务器没有服务器连接返回错误响应,那么你的about文本将不包含任何内容,它将为null ..

尝试在about.text()方法之前声明初始化abouttext并检查about.text是否未将null值设置为abouttext ..

答案 1 :(得分:0)

问题之一是您试图从后台线程更新UI组件

<body>
    <div class="one">
        <div class="two">
          <svg id="cluster" width="700" height="500"></svg>
        </div>
    </div>
</body>

从错误中看,问题出在url或查询url是否不为NULL之前,这是因为在select()中css查询无效,请尝试更改查询。

>

使用断点来了解哪个代码确实使您的应用崩溃