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.