WebView加载栏不起作用

时间:2015-04-27 12:46:43

标签: java android webview

我正在尝试将加载栏添加到WebView中 但它显示错误。

请帮我补充一下。
我在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);
    }
}

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>

1 个答案:

答案 0 :(得分:0)

试试这个:

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