简体   繁体   中英

How to show webview when it's finished loading?

I am working on an android log-in application. When logged in the user will be redirected to a webview. Currently everything is working great, however i want to change what is displayed when loading.

Currently I use ProgressDialog . I figured I could just change the setContentView() and onPageFinished() but it's resulting in a nullPointerException

Here is the current, working method:

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"));
}

And here is what I am trying to do:

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"));
}

Unfortunatly this is resulting in a nullPointerException and I have no idea why. Here is my 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)

Why is this error occuring, and what can I do to solve my problem?

EDIT: the answer below states that webView is not in my loadingscreen, i have no idea what he means by this, i am initializing webView right?? if not what do I do to add it to loadingscreen??

Take a look at this :

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

The error occurred because the webView is not in your loadscreen .

I think the simplest way is to manually show (in onPreExecute ) and dismiss (in onPostExecute ) the ProgressDialog without using setContentView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM