我获得了在webview中显示活动指标的代码。我检查了多个参考文件,但仍然无法使其正常工作。你能帮我调试下面的代码吗?
活动指示符未附带以下代码
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
final BaseActivity MyActivity = ReviewWebActivity.this;
setContentView(R.layout.review_web);
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
ScannedProduct product = getReviewUrl();
reviewUrl = product.getReviewLink();
if (reviewUrl == null) {
String err = product.getErrorCode();
if(err.equals("")) err ="No Data Available for this product";
Toast.makeText(getApplicationContext(),
"No Data Available for this product", 1).show();
return;
}
webReview = (WebView) findViewById(R.id.webReview);
webReview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Make the bar disappear after URL is loaded, and changes
// string to Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 1000); // tried with 100 also
}
});
webReview.setWebViewClient(new ReviewWebClient());
webReview.getSettings().setJavaScriptEnabled(true);
webReview.loadUrl(reviewUrl);
}
答案 0 :(得分:42)
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class SandbarinFacebook extends Activity {
WebView mWebView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...",true);
mWebView = (WebView) findViewById(R.id.webkitWebView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if(pd!=null && pd.isShowing())
{
pd.dismiss();
}
}
});
mWebView.loadUrl("http://www.yahoo.co.in");
setTitle("Yahoo!");
}
}
答案 1 :(得分:4)
在活动的onCreate
方法中写下以下代码。
webView.setWebChromeClient(new ChromeClient());
progress=ProgressDialog.show(this, "", "Loading...");
webView.loadUrl(url);
在同一活动中创建ChromeClient
课程。
private class ChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress >= 85) {
progress.dismiss();
}
super.onProgressChanged(view, newProgress);
}
}
相应地声明对象。回到我身边如果你仍然面临错误。我将提供完整的源代码。
答案 2 :(得分:3)
我无法发表评论,因为我没有足够的声望点,只是对接受的答案进行快速评论:在检查对话框是否显示之前检查null。这将避免可怕的NPE。
if(pd != null && pd.isShowing()) { ... }
答案 3 :(得分:0)
Kotlin片段:
myProgressBar.show()
myWebView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
myProgressBar.hide()
}
}
将此扩展功能添加到扩展文件中:
fun View.show() {
visibility = View.VISIBLE
}
fun View.hide() {
visibility = View.GONE
}