0

我正在尝试通过应用程序将数据从网站获取到我的手机!所以我尝试了使用 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
4

2 回答 2

0

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

我认为你的回报是关于文本;给出问题

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

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

于 2013-09-21T19:14:18.617 回答
0

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

 @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
        //since async works on different thread it cannot update the ui so we need to run the updating task on UI thread
// use the below code wherever you are changing Ui, like in prexcute, postexecute

       runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // Stuff that updates the UI

            }
        });
    }

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

使用断点来了解哪些代码正在使您的应用程序崩溃

于 2018-07-21T14:22:37.623 回答