jsoup抛出空指针异常

时间:2014-09-16 14:00:02

标签: eclipse

我无法找到我输出的错误是什么,它在text1.setText(docEle.toString());抛出空指针异常..Plz help.I已经附加了下面的logcat错误。 -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---- ---------------------------------------------- --------------------------------------------

      package com.example.flashcardsdemo;

    import java.io.InputStream;
    import org.apache.http.HttpResponse;
    import org.apache.http.StatusLine;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.select.Elements;
    import org.w3c.dom.Element;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;

    public class MainActivity extends Activity {
        private TextView text1;
        Document doc;
        Context context;
        Elements docEle;
        String s;
        String result ="";
        String UrlLink = "http://postimg.org/gallery/5y7ulm9o/b2f8235e/";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context=this;
            Gethtml Ght = new Gethtml();
            text1=(TextView)findViewById(R.id.text1);
            Ght.execute();
        }

        public class Gethtml extends AsyncTask {
            ProgressDialog dialog;

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                dialog = new ProgressDialog(context);
                dialog.setTitle("Loading");
                dialog.setMessage("Html....");
                dialog.show();
            }

            @Override
            protected Object doInBackground(Object... params) {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpGet request = new HttpGet(UrlLink);
                    HttpResponse response = client.execute(request);
                    StatusLine line = response.getStatusLine();
                    int statuscode = line.getStatusCode();
                    if (statuscode != 200) {
                        return null;
                    }
                     doc = Jsoup.connect(UrlLink).get();
                     Elements docEle = doc.select("#header a");
   Here----------->  text1.setText(docEle.toString());
                     Log.v("Document", docEle.toString());
                }

                catch (Exception e) {
                    Log.v("Error", e.toString());
                }

                return null;
            }



        @Override
        protected void onPostExecute(Object result) {
            dialog.dismiss();
            //text1.setText(result.toString());
            //System.out.println(doc.html());
            Log.v("Document", docEle.toString());
        }
    }   
}


[1]: http://i.stack.imgur.com/TrSMQ.png

0 个答案:

没有答案