简体   繁体   English

如何在Webview中的OnRecievedError上膨胀新的布局?

[英]How to inflate new layout on OnRecievedError in Webview?

I am trying to inflate new error.xml layout whenever webview catches error and executes onRecievedError() method. 我想,只要夸大新error.xml布局webview捕捉错误并执行onRecievedError()方法。 I am half way through but the problem is i get both the view... my error.xml view as well as *"webpage not found error "*view so please help me with this. 我已完成一半,但问题是我同时获得了两个视图...我的error.xml视图以及*“找不到网页错误” *视图,所以请为此提供帮助。

Fragment.java Fragment.java

public class menu_1 extends Fragment {
private WebView wv;
private TextView tv;
RelativeLayout layout;

@Override
public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_1, container, false);


    wv = (WebView) v.findViewById(R.id.frag1_web);
    wv.loadUrl("file:///res/raw/web1.html");
    WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_LONG).show();


            LayoutInflater inflater1=getActivity().getLayoutInflater();
            View v=inflater1.inflate(R.layout.show_error,null);
            view.addView(v);
       }

});
    return v;
}



@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState)  {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Menu 1");
}


}

show_error.xml show_error.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">

<TextView
    android:id="@+id/errorText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Problem Loading Page"
    android:textColor="@android:color/black"
    android:textSize="24sp" />
</RelativeLayout>

Fragment_main.xml Fragment_main.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">

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

以下是我在跑步中收到的视图。

Update fragment layout 更新片段布局

Fragment_main.xml Fragment_main.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">

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

    <TextView
        android:id="@+id/errorText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone"
        android:text="Problem Loading Page"
        android:textColor="@android:color/black"
        android:textSize="24sp" />

</RelativeLayout>

Hide webview on Error & show text view 隐藏错误的网页视图并显示文本视图

wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        Toast.makeText(getActivity().getApplicationContext(), "Error", Toast.LENGTH_LONG).show();


        webView.setVisibility(View.GONE);
        textView.setVisibility(View.VISIBLE);
   }

});

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

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