错误:加载doInBackground()时

时间:2015-08-26 12:38:18

标签: android android-asynctask

在doInBackground方法加载时,Asynctask中出现了致命异常。如何解决这个问题。

这是OnCreate方法:

                super.onCreate(savedInstanceState);
                setContentView(R.layout.single_list_item);
                reload();
                Intent in = getIntent();
                this.link = in.getStringExtra("link");
                String link = in.getStringExtra(KEY_LINK);
                this.web = (WebView) findViewById(R.id.webView1);
                TextView Desc = (TextView) findViewById(R.id.description_label);
                loadStuff localloadStuff = new loadStuff();
                String[] arrayofString = new String[1];
                arrayofString[0] = this.link;
                localloadStuff.execute(arrayofString);

这是Asyntask Class:

            public class loadStuff extends AsyncTask<String, Void, Void> {
                    public loadStuff() {
                    }
                    @Override
                    protected Void doInBackground(String... params) {
                        SingleItemActivity.this.link = SingleItemActivity.this.link
                                .replace("http://www.", "http://m.");
                        SingleItemActivity.this.web.getSettings()
                                .setJavaScriptEnabled(true);
                        SingleItemActivity.this.web.loadUrl(SingleItemActivity.this.link);
                        SingleItemActivity.this.web.setWebViewClient(new WebViewClient());
                        return null;
                    }

LogCat错误:

    08-26 18:33:35.753: E/AndroidRuntime(14945): FATAL EXCEPTION: AsyncTask #1
    08-26 18:33:35.753: E/AndroidRuntime(14945): Process: com.example.sliding_activity, PID: 14945
    08-26 18:33:35.753: E/AndroidRuntime(14945): java.lang.RuntimeException: An error occured while executing doInBackground()
    08-26 18:33:35.753: E/AndroidRuntime(14945):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    08-26 18:33:35.753: E/AndroidRuntime(14945):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)

3 个答案:

答案 0 :(得分:0)

 public class loadStuff extends AsyncTask<String, Void, Void> {
      public loadStuff() {
      }
      @Override
      protected Void doInBackground(String... params) {
          SingleItemActivity.this.link = SingleItemActivity.this.link
                  .replace("http://www.", "http://m.");
          SingleItemActivity.this.web.getSettings()
                  .setJavaScriptEnabled(true);

          return null;
      }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
         SingleItemActivity.this.web.loadUrl(SingleItemActivity.this.link);
         SingleItemActivity.this.web.setWebViewClient(new WebViewClient());
    }


  }

不要在doInBackground方法中更新UI

答案 1 :(得分:0)

您正尝试在doInBackground()方法中访问webview。你不能在doInBackground()中做任何UI工作。如果你这样做会失败。

请参阅此链接:java.lang.RuntimeException: An error occured while executing doInBackground()

答案 2 :(得分:0)

对于此操作,您并不需要AsyncTask。 您遇到的问题是您正在尝试从后台线程更新UI线程。 事实上,你可以简单地写下这个并且可以工作

super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
reload();
Intent in = getIntent();
this.link = in.getStringExtra("link");
String link = in.getStringExtra(KEY_LINK);
this.web = (WebView) findViewById(R.id.webView1);
TextView Desc = (TextView) findViewById(R.id.description_label);
loadStuff localloadStuff = new loadStuff();
String[] arrayofString = new String[1];
arrayofString[0] = this.link;
localloadStuff.execute(arrayofString);
SingleItemActivity.this.link = SingleItemActivity.this.link
                            .replace("http://www.", "http://m.");
SingleItemActivity.this.web.getSettings()
                            .setJavaScriptEnabled(true);
SingleItemActivity.this.web.loadUrl(SingleItemActivity.this.link);
SingleItemActivity.this.web.setWebViewClient(new WebViewClient());