Reputation: 641
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.
Upvotes: 0
Views: 5098
Reputation: 20961
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();
}
}
}
Upvotes: 4
Reputation: 1034
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);
.....
Upvotes: 1