gpt4 book ai didi

java - 在 Webview 中加载进度条

转载 作者:行者123 更新时间:2023-11-30 00:43:07 24 4
gpt4 key购买 nike

我有问题。我在 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);
}
}

有日志:

    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)

最佳答案

将 progressBar 传递给您的自定义 webview:

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

希望这对您有所帮助。

关于java - 在 Webview 中加载进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com