4

I'm working on a project with "android.support.v4.view.ViewPager". There are 2 tabs and in each of these tabs I want to show a different WebView. Here is the code of one of those tabs and it's layout :

TabOne.java

public class TabOne extends Fragment
{
 WebView myWebView;
 @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
        View view = inflater.inflate(R.layout.tabone, container, false);
            WebView myWebView = (WebView) view.findViewById(R.id.webview);
    myWebView.loadUrl("file:///android_asset/help/index.html");
    return view;
}
}

tabone.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/webview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />

</LinearLayout>

The problem is that in the first tab, the WebView is loaded corectly. On the other Tab, which is exactly the same declared, I can't focus elements and I am getting some Force Closes sometimes. If I'm on the second tab, which has the error, and I switch the orientation to Landscape, the second tab is loaded perfectly but when I switch to the first tab I get the error. Here is the LogCat error :

11-18 09:00:37.460: E/webcoreglue(29444): Should not happen: no rect-based-test nodes found
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
ionutzm05
  • 53
  • 1
  • 4

3 Answers3

1

This appears to be a bug, either in ViewPager or WebView, on Android 4.1 (and perhaps higher).

Alas, I have no workaround for having WebView be on the second or subsequent pages of the pager.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @IamDeveloper: No. Worse, it appears that the `WebView`-in-the-first-page solution does not work reliably either. – CommonsWare Mar 01 '13 at 02:07
  • This is a bug as @CommonsWare links above. Here is a solution I found: http://stackoverflow.com/a/15153292/468360 – Codeversed Mar 01 '13 at 08:22
0

In Java, create MyWebView as sub class of WebView

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        onScrollChanged(getScrollX(), getScrollY(), getScrollX(), getScrollY());
        return super.onTouchEvent(event);
    }
}

In layout xml, using MyWebView instead of WebView

<your.package.MyWebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Tuyen
  • 83
  • 7
0

I have WebView inside a ViewPager and fixed this problem by overriding onTouchEvent() by extending WebView class.

Here is my code:

@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
requestFocusFromTouch(); } return super.onTouchEvent(event); }

Cheers!

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
Audumbar
  • 404
  • 5
  • 16