简体   繁体   中英

webview is not loading url inside the fragment in android

I am new to fragment.I am loading url in webview inside fragment but its not loading .In emulator webpage may temporarily down message is coming .in samsung galaxy tab device coming blank can anybody tell what is problem How to solve

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 WebViewFragment1 extends Fragment {
    WebView loadWeb; 

    @Override       
    public void onCreate(Bundle savedInstanceState) 
    {      
        super.onCreate(savedInstanceState);            
        // Tell the framework to try to keep this fragment around           
        // during a configuration change.           
        setRetainInstance(true);          
 // Start up the worker thread.          

    }
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { 
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.gcmweb, container, false); 
        loadWeb=(WebView)layout.findViewById(R.id.loadWeb); 
        /*String url = "http://unstructure.org/unstruc/unstruc.php?i="
            + "sample test".replaceAll(" ", "-");*/
        String url="http://www.google.com/";
loadWeb.loadUrl(url);       
        return layout; 
        }

}

Here I am adding dynamically in activity

FragmentTransaction ft = fm.beginTransaction();  
         Fragment fragReplace=new WebViewFragment1();
            //fm.beginTransaction(); 
            ft.add(R.id.replace_frag_container,fragReplace);    
            ft.addToBackStack(null);
            ft.commit();

Thanks

For using fragments, you should extend activity and have a layout of fragment or vice versa. Check this link This is a great tutorial for beginners

    public class DetailsFragment extends Fragment {
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    return(inflater.inflate(R.layout.details_fragment, container, false));
  }

  public void loadUrl(String url) {
    ((WebView)(getView().findViewById(R.id.browser))).loadUrl(url);
  }
}

This is working for me can u try this..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM