在AlertDialog里面的webview中自定义WebClient

时间:2018-03-22 07:37:32

标签: android webview alertdialog swiperefreshlayout

我想在WebClient内的网页浏览中制作自定义AlertDialog,但我的代码无效。

问题

  

swipeRefreshLayout显示空对象引用

代码

public class PreviewLatestVideo extends BaseActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private WebView webView;
String url = "http://wwww.google.com";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_premium_video_preview);
    webView = findViewById(R.id.webView);
    swipeRefreshLayout = findViewById(R.id.swipeRefresh);

btnSubscribed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            AlertDialog.Builder dialog = new AlertDialog.Builder(PreviewLatestVideo.this);
            LayoutInflater inflater = (LayoutInflater) PreviewLatestVideo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View viewWeb = inflater.inflate(R.layout.web_charging,null);
            dialog.setView(viewWeb);

            webView.setWebViewClient(new customWebViewClient());
            webView.setInitialScale(350);
            webView.loadUrl(url);

            dialog.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            dialog.show();
        }

    });

private class customWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        swipeRefreshLayout.setRefreshing(true); // <- Here null object reference
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String request) {
        view.loadUrl(request);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        swipeRefreshLayout.setRefreshing(false);
    }
}
}
}

这是我的web_charging.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".PreviewLatestVideo">

<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipeRefresh">

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

</WebView>

对此方面的任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

试试这个

    btnSubscribed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            AlertDialog.Builder dialog = new AlertDialog.Builder(PreviewLatestVideo.this);
            LayoutInflater inflater = (LayoutInflater) PreviewLatestVideo.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View viewWeb = inflater.inflate(R.layout.web_charging,null);
            dialog.setView(viewWeb);
            SwipeRefreshLayout swipeRefreshLayout;
            WebView webView;

            webView=view.findViewById(R.id.webbiew);
            swipeRefreshLayout=view.findViewById(R.id.swipeRefreshLayout);

            webView.setWebViewClient(new customWebViewClient(swipeRefreshLayout));
            webView.setInitialScale(350);
            webView.loadUrl(url);

            dialog.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            dialog.show();
        }

    });

自定义类

private class customWebViewClient extends WebViewClient {

    SwipeRefreshLayout swipeRefreshLayout;

    public customWebViewClient(SwipeRefreshLayout swipeRefreshLayout) {
        this.swipeRefreshLayout = swipeRefreshLayout;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        swipeRefreshLayout.setRefreshing(true); // <- Here null object reference
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String request) {
        view.loadUrl(request);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        swipeRefreshLayout.setRefreshing(false);
    }
}

布局网络充电

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.v4.widget.SwipeRefreshLayout>