简体   繁体   English

WebView加载栏不起作用

[英]WebView Loading Bar is not working

I am trying to add a loading bar into a WebView. 我正在尝试将加载栏添加到WebView中。
But it is showing an error. 但是它显示了一个错误。

Please help me in adding that. 请帮我补充一下。
I am using the following code in my MainActivity.java 我在MainActivity.java使用以下代码

package com.example.apps;

import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.example.com?ref=app");
        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {

        super.onPageStarted(view, url, favicon);
        findViewById(R.id.progress1).setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        findViewById(R.id.progress1).setVisibility(View.GONE);
    }


    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.exit) {
            System.exit(0);
        }
        if(id==R.id.home)
        {
            mWebView.loadUrl("http://www.example.com?ref=app");

        } if(id==R.id.forum)
        {
            mWebView.loadUrl("http://www.example.com/forum");

        } if(id==R.id.dir)
        {
            mWebView.loadUrl("http://www.example.com/webdir");

        } if(id==R.id.article)
        {
            mWebView.loadUrl("http://www.example.com/articles");

        } if(id==R.id.edu)
        {
            mWebView.loadUrl("http://www.example.com/education");

        } if(id==R.id.about)
        {
            mWebView.loadUrl("http://www.example.com/about");

        }


        return super.onOptionsItemSelected(item);
    }
}

And the activity_main.xml has following code 并且activity_main.xml具有以下代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    tools:ignore="MergeRootFrame">

    <WebView
        android:id="@+id/activity_main_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ProgressBar
        android:id="@+id/progress1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

try this: 尝试这个:

webView.setWebChromeClient(new WebChromeClient(){

     public void onProgressChanged(WebView view, int progress) {
             self.setTitle("Loading...");
             self.setProgress(progress * 100);
                if(progress == 100)
                   self.setTitle("write your tittle here");
             }
});

webView.loadUrl(URL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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