0
package com.example.application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.example.androidtablayout.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class PhotosActivity extends Activity  {

    public void onCreate(Bundle savedInstanceState) {


        String htmlCode = "";

            Document doc;
              try {
                  //Problem start
            doc = Jsoup.connect("http://www.example.com/").get();
            Element content = doc.select("a").first();
                    String variable = content.text();
                  // Problem end
                    TextView t = new TextView(this);
                    t=(TextView)findViewById(R.id.textView3); 
                    t.setText(variable);
               } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }

            super.onCreate(savedInstanceState);
            setContentView(R.layout.photos_layout);

    }
}

My problem is with JSoup framework. Between of "Problem" lines. I take the "Unexpectedly Stopped Error".

When I put the "htmlCode" variable like this

t.SetText(htmlCode); 

It is work but i get all of html source code .

What is the main problem , i can't get it.

cyildirim
  • 614
  • 1
  • 8
  • 20

2 Answers2

4

There's a lot wrong with your code:

  • super.onCreate() should always be the very first call in your own onCreate()

  • You call setContentView() at the very end but try to findViewById() before that in the catch clause. At that point, your Activity has no content view yet, so findViewById() will return null -- the resulting NullPointerException is most likely why your code crashes.

  • You do IO on the UI thread. Don't do that, Android will force-quit your app because IO is unpredictable and your app will be unresponsive, this is even worse with network IO. Check out AsyncTask on how to do IO off the UI thread and then asynchronously update your TextView.

Also, check out logcat. For most crashes, it will contain a stacktrace that will pinpoint where things went wrong.


Here's a reworked version of your code that follows some of the advice I've given.

Please note that I did not have the time to fix the most important issue: This code still does IO on the UI thread. It should use an AsyncTask instead!

public class PhotosActivity extends Activity {

  // Use a string constant to "tag" log statements that belong to the same
  // feature/module of your app. This activity does something with "photos" so
  // use that as a tag. It's the first parameter to Android's Log methods.
  private static final String TAG = "photos";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call super.onCreate() first.
    setContentView(R.layout.photos_layout); // Then load the layout.

    // Find textView3 in layout and set it's text to HTML code.
    // There's no need to create a new TextView() here.
    TextView textView = (TextView) findViewById(R.id.textView3);
    textView.setText(getHtmlCode());
  }

  // Structure your code. If it's a larger block that does one thing,
  // extract it into a method.
  private String getHtmlCode() {
    try {
      Document doc = Jsoup.connect("http://www.example.com/").get();
      Element content = doc.select("a").first();
      return content.text();
    } catch (IOException e) {
      // Never e.printStackTrace(), it cuts off after some lines and you'll
      // lose information that's very useful for debugging. Always use proper
      // logging, like Android's Log class, check out
      // http://developer.android.com/tools/debugging/debugging-log.html
      Log.e(TAG, "Failed to load HTML code", e);
      // Also tell the user that something went wrong (keep it simple,
      // no stacktraces):
      Toast.makeText(this, "Failed to load HTML code",
          Toast.LENGTH_SHORT).show();
    }
  }
}
Community
  • 1
  • 1
Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65
  • Hey, Phillipp. Thanks for answer. The point of i don't understand, when I delete between //problem start and // problem end and put the string variable at "setText()" I take the variable at the my layout. Should I work out of onCreate event ? Did you mean ? – cyildirim Sep 11 '12 at 06:33
  • I'm sorry, I don't understand your comment at all. I've quickly reworked your code and added it to my question -- I hope this helps. Please note that the code still does not use `AsyncTask`, but it should! – Philipp Reichart Sep 11 '12 at 07:06
  • Thanks Phillip! I worked for 2 days for this code. I understand my all mistakes. Thanks again :) – cyildirim Sep 11 '12 at 07:30
  • @ShabbirDhangot, please post a new question, include your code and the stacktrace. – Philipp Reichart Jan 22 '14 at 18:55
1

You should move your call super.onCreate(...) and setContentView(...) to the first two lines in your onCreate method.

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.photos_layout);
  .....
Zyber
  • 1,034
  • 8
  • 19