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);

    }
}

我的问题是 JSoup 框架。在“问题”行之间。我采取“意外停止错误”。

当我像这样放置“htmlCode”变量时

t.SetText(htmlCode); 

这是工作,但我得到了所有的 html 源代码。

主要问题是什么,我无法理解。

4

2 回答 2

3

你的代码有很多问题:

  • super.onCreate()应该永远是你自己的第一个电话onCreate()

  • 您在最后调用,但在 catch 子句中setContentView()尝试在此之前调用。findViewById()那时,您Activity还没有内容视图,因此findViewById()将返回null- 结果NullPointerException很可能是您的代码崩溃的原因。

  • 您在 UI 线程上执行 IO。不要那样做,Android 会强制退出你的应用,因为 IO 是不可预测的,你的应用会无响应,网络 IO 更糟糕。查看AsyncTask,了解如何从 UI 线程执行 IO,然后异步更新您的TextView.

另外,请查看logcat。对于大多数崩溃,它将包含一个堆栈跟踪,可以查明哪里出了问题。


这是您的代码的修改版本,它遵循了我给出的一些建议。

请注意,我没有时间解决最重要的问题:这段代码仍然在 UI 线程上执行 IO。它应该使用AsyncTask代替!

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();
    }
  }
}
于 2012-09-10T13:59:55.310 回答
1

您应该将呼叫移动super.onCreate(...)setContentView(...)onCreate 方法的前两行。

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.photos_layout);
  .....
于 2012-09-10T14:01:50.923 回答