java - WebView Placed in a Fragment doesn't load -


let's start code. main activity:

package it.mdibonito.futuroremoto;    import android.app.activity;  import android.graphics.bitmap;  import android.support.design.widget.tablayout;  import android.support.design.widget.floatingactionbutton;  import android.support.design.widget.snackbar;  import android.support.v7.app.appcompatactivity;  import android.support.v7.widget.toolbar;    import android.support.v4.app.fragment;  import android.support.v4.app.fragmentmanager;  import android.support.v4.app.fragmentpageradapter;  import android.support.v4.view.viewpager;  import android.os.bundle;  import android.view.keyevent;  import android.view.layoutinflater;  import android.view.menu;  import android.view.menuitem;  import android.view.view;  import android.view.viewgroup;    import android.webkit.websettings;  import android.webkit.webview;  import android.webkit.webviewclient;  import android.widget.textview;    public class mainactivity extends appcompatactivity {        /**       * {@link android.support.v4.view.pageradapter} provide       * fragments each of sections. use       * {@link fragmentpageradapter} derivative, keep every       * loaded fragment in memory. if becomes memory intensive,       * may best switch       * {@link android.support.v4.app.fragmentstatepageradapter}.       */      private sectionspageradapter msectionspageradapter;        /**       * {@link viewpager} host section contents.       */      private viewpager mviewpager;        @override      protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);          setcontentview(r.layout.activity_main);          toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);          setsupportactionbar(toolbar);          // create adapter return fragment each of 3          // primary sections of activity.          msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager());            // set viewpager sections adapter.          mviewpager = (viewpager) findviewbyid(r.id.container);          mviewpager.setadapter(msectionspageradapter);            tablayout tablayout = (tablayout) findviewbyid(r.id.tabs);          tablayout.setupwithviewpager(mviewpager);          //set icons tabs after  tablayout.setupwithviewpager(viewpager)          int number_of_tabs = 5; //this fixed number of tabs in tablayout, returned *sectionspageradapter.getcount()*          final int[] tabicons = new int[]{r.drawable.ic_format_list_bulleted_white_48dp, r.drawable.ic_add_location_white_48dp, r.drawable.ic_public_white_48dp, r.drawable.ic_share_white_48dp, r.drawable.ic_info_white_24dp};          for(int i=0;i<number_of_tabs;i++){              tablayout.gettabat(i).seticon(tabicons[i]);          }            floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);          fab.setonclicklistener(new view.onclicklistener() {              @override              public void onclick(view view) {                  snackbar.make(view, "replace own action", snackbar.length_long)                          .setaction("action", null).show();              }          });        }          @override      public boolean oncreateoptionsmenu(menu menu) {          // inflate menu; adds items action bar if present.          getmenuinflater().inflate(r.menu.menu_main, menu);          return true;      }        @override      public boolean onoptionsitemselected(menuitem item) {          // handle action bar item clicks here. action bar          // automatically handle clicks on home/up button, long          // specify parent activity in androidmanifest.xml.          int id = item.getitemid();            //noinspection simplifiableifstatement          if (id == r.id.action_settings) {              return true;          }            return super.onoptionsitemselected(item);      }        /**       * placeholder fragment containing simple view.       */      public static class placeholderfragment extends fragment {          /**           * fragment argument representing section number           * fragment.           */          private static final string arg_section_number = "section_number";            public placeholderfragment() {          }            /**           * returns new instance of fragment given section           * number.           */          public static placeholderfragment newinstance(int sectionnumber) {              placeholderfragment fragment = new placeholderfragment();              bundle args = new bundle();              args.putint(arg_section_number, sectionnumber);              fragment.setarguments(args);              return fragment;          }            @override          public view oncreateview(layoutinflater inflater, viewgroup container,                                   bundle savedinstancestate) {              if (getarguments().getint(arg_section_number) == 1) {                  view rootview = inflater.inflate(r.layout.fragment_programma, container, false);                  return rootview;              } else {                  if (getarguments().getint(arg_section_number) == 2) {                      view rootview = inflater.inflate(r.layout.fragment_programma, container, false);                      return rootview;                  } else if (getarguments().getint(arg_section_number) == 3) {                      view rootview = inflater.inflate(r.layout.fragment_programma, container, false);                      return rootview;                  } else if (getarguments().getint(arg_section_number) == 4) {                      view rootview = inflater.inflate(r.layout.fragment_programma, container, false);                      return rootview;                  } else if (getarguments().getint(arg_section_number) == 5) {                      view rootview = inflater.inflate(r.layout.fragment_programma, container, false);                        return rootview;                  } else {                      view rootview = inflater.inflate(r.layout.fragment_main, container, false);                      return rootview;                  }              }          }      }        /**       * {@link fragmentpageradapter} returns fragment corresponding       * 1 of sections/tabs/pages.       */      public class sectionspageradapter extends fragmentpageradapter {            public sectionspageradapter(fragmentmanager fm) {              super(fm);          }            @override          public fragment getitem(int position) {              // getitem called instantiate fragment given page.              // return placeholderfragment (defined static inner class below).              return placeholderfragment.newinstance(position + 1);          }            @override          public int getcount() {              // show 3 total pages.              return 5;          }            @override          public charsequence getpagetitle(int position) {              switch (position) {                  case 0:                      return "programma";                  case 1:                      return "mappe";                  case 2:                      return "domes";                  case 3:                      return "social";                  case 4:                      return "info";              }              return null;          }      }  }

this programma.java

package it.mdibonito.futuroremoto;      import android.os.bundle;  import android.support.v4.app.fragment;  import android.view.layoutinflater;  import android.view.view;  import android.view.viewgroup;  import android.webkit.websettings;  import android.webkit.webview;  import android.widget.textview;    /**   * simple {@link fragment} subclass.   */  public class programma extends fragment {          public programma() {          // required empty public constructor      }          @override      public view oncreateview(layoutinflater inflater, viewgroup container,                               bundle savedinstancestate) {            view rootview = inflater.inflate(r.layout.fragment_programma, container, false);          webview heroespage = (webview) rootview.findviewbyid(r.id.progwebview);          websettings websettings = heroespage.getsettings();          websettings.setjavascriptenabled(true);          heroespage.loadurl("http://www.google.it/");            return rootview;      }

fragment_programma.xml

<framelayout 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"      tools:context="it.mdibonito.futuroremoto.programma">        <!-- todo: update blank fragment layout -->        <webview          android:layout_width="match_parent"          android:layout_height="match_parent"          android:id="@+id/progwebview"          android:layout_gravity="left|top" />    </framelayout>

more details: i'm using standard android studio 2.1.3 tabbed activity style, , prefer keep it.as title, problem webview doesn't load anything, display white webview. of course, i've added permissions use internet android manifest... help?

1. in mywebclient you've overriden shouldoverrideurlloading() , return true.

this means don't want webview handle url, , you'll handle yourself.

if want webview load requested url, return false in `shouldoverrideurlloading(). or better, don't override @ all.

2. sure programma class started? activity, not fragment. use same layout (fragment_main) in both programmaactivity , in placeholderfragment. however, in programmaactivity loading url. placeholderfragment nothing webview

3. how placeholderfragment should like

public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number";

    public placeholderfragment() {     }      /**      * returns new instance of fragment given section      * number.      */     public static placeholderfragment newinstance(int sectionnumber) {         placeholderfragment fragment = new placeholderfragment();         bundle args = new bundle();         args.putint(arg_section_number, sectionnumber);         fragment.setarguments(args);         return fragment;     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {          view rootview = inflater.inflate(r.layout.fragment_programma, container, false);         webview heroespage = (webview) rootview.findviewbyid(r.id.progwebview);         websettings websettings = heroespage.getsettings();         websettings.setjavascriptenabled(true);         heroespage.loadurl("http://www.google.it/");          return rootview;     } } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -