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

  • setContentView()最後に呼び出しますがfindViewById()、その前に catch 句で呼び出します。その時点でActivityはまだコンテンツ ビューがないため、findViewById()返されます。nullその結果NullPointerException、コードがクラッシュする可能性が最も高くなります。

  • UI スレッドで IO を実行します。IO は予測不能であり、アプリは応答しなくなるため、Android はアプリを強制終了します。これは、ネットワーク IO ではさらに悪化します。UI スレッドから IO を実行する方法についてAsyncTaskを確認してから、 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 メソッドの最初の 2 行に移動する必要があります。

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