-1

I have got the source code for sliding menu and want to implement a webview into this, However it doesn't seem to work and can't find a clear answer on how to do this. Here is my code and hopefully it will make sense on what i am trying to achieve:

import info.androidhive.slidingmenu.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class TestingPage extends Fragment {

public TestingPage(){}  //Have removed this code, but still gives same error
private WebView webView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

 View rootView = inflater.inflate(R.layout.fragment_testpage, container);

 webView = (WebView) findViewById(R.id.Webview1);
 webView.getSettings().setBuiltInZoomControls(true);
 webView.getSettings().setLoadWithOverviewMode(true);
 webView.getSettings().setUseWideViewPort(true);
 webView.loadUrl("file:///android_asset/index.html");

 return rootView;

}


}

Now the problem i have is under findViewById i have an error with the option of: "Create method findViewById(int)

When i create this (via the option given), i then get no error and then this is what i get:

private WebView findViewById(int webview1) {
    // TODO Auto-generated method stub
    return null;
   }
 }

Now i am not sure what to do from here? when i run it and test the app i get this:

 09-11 13:39:07.649: E/AndroidRuntime(31488): FATAL EXCEPTION: main
 09-11 13:39:07.649: E/AndroidRuntime(31488): java.lang.NullPointerException
 09-11 13:39:07.649: E/AndroidRuntime(31488):   at  com.test.testapp.findViewById(TestingPage.java:33)
 09-11 13:39:07.649: E/AndroidRuntime(31488):   at com.test.testapp.onCreateView(TestingPage.java:22)
 09-11 13:39:07.649: E/AndroidRuntime(31488):   at android.app.Fragment.performCreateView(Fragment.java:1699)
 09-11 13:39:07.649: E/AndroidRuntime(31488):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
 09-11 13:39:07.649: E/AndroidRuntime(31488):   at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
 09-11 13:47:58.229: E/AndroidRuntime(32545): FATAL EXCEPTION: main 09-11 13:47:58.229: E/AndroidRuntime(32545): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first

Edited

XML file:

<?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/Webview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</RelativeLayout>

EDITED WITH CORRECT CODE

The problem was at this code:

View rootView = inflater.inflate(R.layout.fragment_campsites, container);

How i resolved the issue was adding false at the end:

View rootView = inflater.inflate(R.layout.fragment_campsites, container, false);

Everything is working fine now.

3 Answers3

1

replace your code with this code

View rootView = inflater.inflate(R.layout.fragment_testpage, container);

webView = (WebView) rootView.findViewById(R.id.Webview1);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("file:///android_asset/index.html");

just change webView = (WebView) rootView.findViewById(R.id.Webview1); line

Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
0

you are webview inside the rootview and there id is inside the root view

View rootView = inflater.inflate(R.layout.fragment_testpage, container);
webView = (WebView) rootView .findViewById(R.id.Webview1);
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
  • Thank you so much that took care of the error but now when running the app i get this: –  Sep 11 '14 at 11:48
  • 09-11 13:47:58.229: E/AndroidRuntime(32545): FATAL EXCEPTION: main 09-11 13:47:58.229: E/AndroidRuntime(32545): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. –  Sep 11 '14 at 11:49
  • R.layout.fragment_testpage post xml – Naveen Tamrakar Sep 11 '14 at 11:50
  • I have posted the xml file as well –  Sep 11 '14 at 12:02
  • Remove this line and post now error public TestingPage(){} – Naveen Tamrakar Sep 11 '14 at 12:03
  • Have tried as suggested, but is giving same error as stated above: The specified child already has a parent. You must call removeView() on the child's parent first" –  Sep 11 '14 at 12:23
  • 09-11 13:47:58.229: E/AndroidRuntime(32545): FATAL EXCEPTION: main 09-11 13:47:58.229: E/AndroidRuntime(32545): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. –  Sep 11 '14 at 12:33
0

change
View rootView = inflater.inflate(R.layout.fragment_testpage, container);

to

View rootView = inflater.inflate(R.layout.fragment_testpage, container,false);

absence of "false" parameter in inflate method will result in adding "rootView" to "container". Which will later cause the exception when returning "rootView" in onCreateView method

dor506
  • 5,246
  • 9
  • 44
  • 79