2017-04-05 45 views
1

我試圖充氣新的error.xml佈局每當webview捕獲錯誤並執行onRecievedError()方法。我半路過,但問題是我得到了這兩個視圖...我的error.xml視圖以及*「網頁找不到錯誤」*查看所以請幫我這個。如何在Webview中對OnRecievedError充氣新佈局?

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

<?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

<?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> 

Below is the view i am receiving on the run.

+0

你可以得到錯誤調用新的活動,並設置error.xml它 –

+0

您使用webview的片段的佈局文件 – Pehlaj

+0

@Hasmukhkachhatiya我得到這個webview裏面的片段,所以不想調用新的活動ju st顯示錯誤... – Rahul

回答

1

更新分段佈局

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> 

隱藏的WebView

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

}); 
+0

Thanax很多@Pehlaj :)它的工作......我可以玩與ImageView和其他小部件使用相同的方法。並在需要時設置可見性。這些東西也可以工作。 – Rahul

+0

是的,你可以基於webview的成功/錯誤顯示/隱藏任何視圖 – Pehlaj