在Webview中加载ProgresBar

时间:2017-02-14 20:45:41

标签: java android

我有问题。 我在main2activity中做了一个进度条。 应用程序启动,但当它加载的网站进度条没有消失。 我的应用程序崩溃了。 请帮忙 ! :* 我想更正我的代码,因为这是了解错误的最佳方法。 谢谢!

有代码:

    package musial.pzstis.com.pzstiz;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/*
 * Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but  that URL should open in the same screen.

- Load WebView with progress bar
 */
public class Main2Activity extends Activity {
    /** Called when the activity is first created. */

    WebView myWebView;
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        myWebView = (WebView) findViewById(R.id.webView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar7);

        myWebView.setWebViewClient(new myWebClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = myWebView.getSettings();
        myWebView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fpzstiz.swiebodzin.pl%2Fzastepstwa%2FPZSTiZ_zastepstwa.pdf");
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

有LOGS:

    02-14 20:43:38.592 20262-20299/musial.pzstis.com.pzstiz E/EGL_emulation: tid 20299: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-14 20:43:44.672 20262-20262/musial.pzstis.com.pzstiz E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: musial.pzstis.com.pzstiz, PID: 20262
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
                                                                              at musial.pzstis.com.pzstiz.Main2Activity$myWebClient.onPageFinished(Main2Activity.java:66)
                                                                              at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:531)
                                                                              at org.chromium.android_webview.AwContentsClientCallbackHelper$MyHandler.handleMessage(AwContentsClientCallbackHelper.java:188)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
02-14 20:43:44.777 1569-4538/system_process E/EGL_emulation: tid 4538: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)

2 个答案:

答案 0 :(得分:1)

将progressBar传递给您的自定义网页视图:

myWebView.setWebViewClient(new myWebClient(progressBar));

将myWebClient类更改为:

public class myWebClient extends WebViewClient {
     private ProgressBar progressBar;

    public myWebClient(ProgressBar progressBar) {
        this.progressBar=progressBar;
        progressBar.setVisibility(View.VISIBLE);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.GONE);
    }
}

希望这有帮助。

答案 1 :(得分:0)

嗯,你有一个带有进度条的空指针异常,我会以这种形式封装对进度条的每次访问:

if(progressBar == null)
    progressBar = (ProgressBar) findViewById(R.id.progressBar7);
if(progressBar)
    progressBar.setVisibility(View.GONE);

在vour代码中实现:

public class Main2Activity extends Activity {
/** Called when the activity is first created. */

WebView myWebView;
ProgressBar progressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    myWebView = (WebView) findViewById(R.id.webView);
    progressBar = (ProgressBar) findViewById(R.id.progressBar7);

    myWebView.setWebViewClient(new myWebClient(this));
    myWebView.getSettings().setJavaScriptEnabled(true);
    WebSettings webSettings = myWebView.getSettings();
    myWebView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fpzstiz.swiebodzin.pl%2Fzastepstwa%2FPZSTiZ_zastepstwa.pdf");
    webSettings.setBuiltInZoomControls(true);
    webSettings.setSupportZoom(true);
    myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);

}

public class myWebClient extends WebViewClient
{

    privat Activity main2ac;
    ProgressBar progressBar;

    public myWebClient(Activity handle){
        main2ac = handle;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub

        if(progressBar == null)
            progressBar = (ProgressBar)main2ac.findViewById(R.id.progressBar7);
        if(progressBar)
            progressBar.setVisibility(View.VISIBLE);
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        if(progressBar == null)
            progressBar = (ProgressBar)main2ac.findViewById(R.id.progressBar7);
        if(progressBar)
            progressBar.setVisibility(View.GONE);
    }
}