如何在完成加载后显示webview?

时间:2014-12-05 14:53:07

标签: java android webview

我正在开发一个Android登录应用程序。登录后,用户将被重定向到webview。目前一切都很好,但我想改变加载时显示的内容。

目前我使用ProgressDialog。我想我可以更改setContentView()onPageFinished(),但它会导致nullPointerException

以下是目前的工作方法:

private void goToSite() throws IOException {

    //Create and start the 'loading' animation
    final ProgressDialog progDailog = ProgressDialog.show(this, "Gegevens       controleren","Een ogenblik geduld...", true);

    //Initialize the webview and set the contentView to display it
    String url=VarControl.DEFAULT_LINK;
    setContentView(R.layout.webview);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient() {
        //This inner method dimisses the 'loading' dialog when the page is fully loaded
        public void onPageFinished(WebView view, String url) {
            progDailog.dismiss();
        }
    });

    //Collect the login data and post it to the server
    String postData = "token="+loadToken()+"&deviceid="+VarControl.regid+"&webview=true"+"&devicetype=Android";

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setDomStorageEnabled(true);
    webView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));
}

以下是我要做的事情:

private void goToSite() throws IOException {

    //Initialize the webview and set the contentView to display loadingscreen
    String url=VarControl.DEFAULT_LINK;
    setContentView(R.layout.loadingscreen);//Changed this
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient() {
        //show the webview when the page is fully loaded
        public void onPageFinished(WebView view, String url) {
           setContentView(R.layout.webview);// And changed this
        }
    });

    //Collect the login data and post it to the server
    String postData = "token="+loadToken()+"&deviceid="+VarControl.regid+"&webview=true"+"&devicetype=Android";

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setDomStorageEnabled(true);
    webView.postUrl(url, EncodingUtils.getBytes(postData, "base64"));
}

不幸的是,这导致nullPointerException,我不明白为什么。 这是我的stackTrace:

12-05 09:40:49.680    1514-1514/com.alserdamedia.wiib E/AndroidRuntime﹕ FATAL EXCEPTION:     main
Process: com.alserdamedia.wiib, PID: 1514
java.lang.RuntimeException: Could not dispatch event: class     com.alserdamedia.wiib.AsyncTaskResultEvent to handler [EventHandler public void             com.alserdamedia.wiib.MainActivity.onAsyncTaskResult(com.alserdamedia.wiib.AsyncTaskResultEvent) 
throws java.io.IOException]: null
        at com.squareup.otto.Bus.throwRuntimeException(Bus.java:456)
        at com.squareup.otto.Bus.dispatch(Bus.java:386)
        at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:367)



        at com.squareup.otto.Bus.post(Bus.java:336)
        at com.alserdamedia.wiib.MyAsyncTask.onPostExecute(MyAsyncTask.java:103)
        at com.alserdamedia.wiib.MyAsyncTask.onPostExecute(MyAsyncTask.java:22)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.alserdamedia.wiib.MainActivity.goToSite(MainActivity.java:96)
at com.alserdamedia.wiib.MainActivity.onAsyncTaskResult(MainActivity.java:71)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.squareup.otto.EventHandler.handleEvent(EventHandler.java:89)
at com.squareup.otto.Bus.dispatch(Bus.java:384)
at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:367)
at com.squareup.otto.Bus.post(Bus.java:336)
at com.alserdamedia.wiib.MyAsyncTask.onPostExecute(MyAsyncTask.java:103)
at com.alserdamedia.wiib.MyAsyncTask.onPostExecute(MyAsyncTask.java:22)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

为什么会出现此错误,我该怎么做才能解决问题?

编辑:下面的答案表明webView不在我的装载屏幕中,我不知道他的意思是什么,我正在初始化webView吗?如果不是我该怎么做才能将它添加到loadingscreen ??

1 个答案:

答案 0 :(得分:2)

看看这个:

setContentView(R.layout.loadingscreen);//Changed this
webView = (WebView) findViewById(R.id.webView);

发生错误是因为webView中没有loadscreen

我认为最简单的方法是在不使用onPreExecute

的情况下手动显示(在onPostExecute中)并关闭ProgressDialog} setContentView} {{1}}