0

I have a webview defined in fragment_main.xml. Is there any sample code to load a Url in onCreateView? My code looks like this:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("Banana", "inside onCreate");

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    // Find that webView1
    WebView mypointer = (WebView) rootView.findViewById(R.id.webView1);
    // Open asset/index.html
    if (mypointer != null) {
        mypointer.loadUrl("file://android_asset/index.html");
    } else {
        Log.v("Banana", "my pointer is null");
    }

    return rootView;
}

However eclipse complains about the second override and when I remove it it runs but does not show the url. The pointer returns null.

my fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="apps101.javabook.MainActivity$PlaceholderFragment" >
<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

How can I load the URL in the app? Thanks.

Update: I found the problem. I was trying to duplicate onCreateView. Erased the duplicate and now it seems to work fine (and yes, forgot a slash too). Problem solved. Thanks.

msapaydin
  • 23
  • 6
  • After looking at [this post](http://stackoverflow.com/questions/5812277/open-local-html-file-in-webview-android), you should be using three forward slashes (i.e. `file:///etc...`) instead of two. Hope that helps – Andrew Schuster Jun 20 '14 at 13:24

1 Answers1

3

use this,

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    // Find that webView1
    WebView mypointer = (WebView) rootView.findViewById(R.id.webView1);
    // Open asset/index.html
    if (mypointer != null) {
        mypointer.loadUrl("file:///android_asset/index.html");
    } else {
        Log.v("Banana", "my pointer is null");
    }

    return rootView;
}

you have missed out a '/' it is file:///

Jithu
  • 1,478
  • 1
  • 13
  • 21