簡體   English   中英

完成加載后如何顯示webview?

[英]How to show webview when it's finished loading?

我正在使用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嗎? 如果不是,我該怎么做才能將其添加到加載屏幕?

看看這個 :

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

發生錯誤是因為webView不在您的loadscreen

我認為最簡單的方法是在不使用setContentView情況下手動顯示(在onPreExecute )和關閉(在onPostExecuteProgressDialog

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM