2

我正在尝试使用 asyntask 在后台加载 webview,同时显示启动画面,当 webview 完成加载时,我希望启动画面消失并显示 webview。由于某种原因,当我尝试在模拟器中编译和运行代码时,我在 LogCat 中遇到了很多错误。

MainActivity.java

package dk.zerone.vuc;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

public final boolean networkCheck() {
    ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
        return true;
    } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {
        return false;
    }
    return false;
}

// The definition of our task class
   private class LoadWebView extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
      super.onPreExecute();
   }

   protected String doInBackground(String... params) {
      String url = params[0];
       WebView webview = new WebView(MainActivity.this);
       webview.loadUrl(url);
       Log.i("WebView", "doInBackground: Done loading url.");
      return "";
   }

   protected void onPostExecute(String result) {
      super.onPostExecute(result);

      }
   }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Splash screen */
    setContentView(R.layout.splash);

    if(networkCheck()) {

        //Instantiating the webview, occupies the whole screen.
        //WebView webview = new WebView(this);
        //webview.loadUrl("http://mobil.vucfyn.dk/mobil");

        new LoadWebView().execute("http://mobil.vucfyn.dk/mobil");
        setContentView(R.layout.webview);
        Log.i("WebView", "Execute asyntask");

        /*webview.setWebViewClient(new WebViewClient() {
              @Override
              public void onPageFinished(WebView view, String url) {
                //super.onPageFinished(view, url);
                  Log.i("WebView", "Finished loading.");
                setContentView(R.layout.webview);
              }
            });*/

    } else {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Fejl");
        alertDialog.setMessage("Ingen forbindelse til internettet");

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        alertDialog.show();
    }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
} 
}

来自 LogCat 的错误消息:

08-25 11:32:11.544: D/dalvikvm(549): GC_FOR_ALLOC freed 67K, 3% free 10191K/10503K, paused 60ms
08-25 11:32:11.563: I/dalvikvm-heap(549): Grow heap (frag case) to 11.036MB for 1048592-byte allocation
08-25 11:32:11.633: D/dalvikvm(549): GC_CONCURRENT freed 1K, 4% free 11214K/11591K, paused 6ms+3ms
08-25 11:32:11.713: I/WebView(549): SetContentView webview
08-25 11:32:11.733: I/WebView(549): Execute asyntask
08-25 11:32:11.923: D/gralloc_goldfish(549): Emulator without GPU emulation detected.
08-25 11:32:12.593: W/dalvikvm(549): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
08-25 11:32:12.646: E/AndroidRuntime(549): FATAL EXCEPTION: AsyncTask #1
08-25 11:32:12.646: E/AndroidRuntime(549): java.lang.RuntimeException: An error occured while executing doInBackground()
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.lang.Thread.run(Thread.java:856)
08-25 11:32:12.646: E/AndroidRuntime(549): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.os.Handler.<init>(Handler.java:121)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView$PrivateHandler.<init>(WebView.java:8211)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView.<init>(WebView.java:437)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView.<init>(WebView.java:1040)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView.<init>(WebView.java:1029)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView.<init>(WebView.java:1019)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.webkit.WebView.<init>(WebView.java:1010)
08-25 11:32:12.646: E/AndroidRuntime(549):  at dk.zerone.vuc.MainActivity$LoadWebView.doInBackground(MainActivity.java:42)
08-25 11:32:12.646: E/AndroidRuntime(549):  at dk.zerone.vuc.MainActivity$LoadWebView.doInBackground(MainActivity.java:1)
08-25 11:32:12.646: E/AndroidRuntime(549):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-25 11:32:12.646: E/AndroidRuntime(549):  at java.util.concurrent.FutureTask$Sync.innerRun  (FutureTask.java:305)
08-25 11:32:12.646: E/AndroidRuntime(549):  ... 5 more
08-25 11:32:16.363: I/Process(549): Sending signal. PID: 549 SIG: 9

我不知道出了什么问题,非常感谢任何帮助。

更新:

package dk.zerone.vuc;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

public final boolean networkCheck() {
    ConnectivityManager connec =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
    connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
    connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
        return true;
    } else if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {
        return false;
    }
    return false;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Splash screen */
    setContentView(R.layout.splash);

    if(networkCheck()) {

        String url = "http://mobil.vucfyn.dk/mobil";

        final WebView webview; 
        webview = new WebView(MainActivity.this);

        webview.loadUrl(url); 

        webview.setWebViewClient(new WebViewClient() { 
            @Override 
            public boolean shouldOverrideUrlLoading(WebView view, String url) {     
                //view.loadUrl(url);     
                return false;
            } 

            @Override 
            public void onPageFinished(WebView view, String url) { 
                super.onPageFinished(view, url); 
                setContentView(webview);
             } 
        });

    } else {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Fejl");
        alertDialog.setMessage("Ingen forbindelse til internettet");

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        alertDialog.show();
    }
  }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
} 
}
4

3 回答 3

2

你为什么使用AsyncTask这可以很容易地通过这个来实现......

最好用这个

WebView webview = new WebView(MainActivity.this);
webview.loadUrl(url);

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {    
        view.loadUrl(url);    
         return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

           //------>> EXIT YOUR SPLASH SCREEN HERE
     }
});
于 2012-08-25T11:57:36.480 回答
2

问题是你甚至在非 ui 线程中初始化 webview。将初始化部分移到 doinbackground 之外。将行下移到 OnPreexcecute 最好还是将其移到 Oncreate

WebView webview;

webview = new WebView(MainActivity.this);

编辑:

对于编辑后的代码,webview 没有显示,因为您没有设置正确的内容

采用

setContentView(webview);

内页已完成

于 2012-08-25T12:21:50.133 回答
2

启用 JavaScript 就成功了。

    webview.getSettings().setJavaScriptEnabled(true);
于 2012-08-29T11:46:37.153 回答