java - fragments not displaying in framelayout -


intro:

to put things perspective, attempting use fragments create sudoku game.

the sudoku board consists of 9 sub-grids, each containing 9 cells, 9 cells in 1 grid, , 9 of these grids form sudoku board, arranged in 3x3 fashion.

enter image description here


method:

home screen containing information, of gridlayout contains 9 framelayouts each sudoku fragment placed into

the 9 sudoku fragments make sudoku board.

sudoku_cell extends linearlayout onclicklistener, since extending textview, unable inflate sudoku_cell layout (is possible so?)

in main activity, have gridlayout containing 9 framelayouts. these layouts have column , row locations set form 3x3 matrix, each of fragments added into.

these framelayouts named on 0-based index: frame00, frame01, frame02, frame10, etc


documentation says:

as mentioned here on developer.android.com, required have an:

  • inflator method fragment, i.e. oncreateview() inflates fragment
  • a layout container of sorts root/place fragment in, in main activity (in case)
  • a fragmentmanager , fragmenttransactionmanager handle fragments, adding , commiting them.

problem:

tl;dr: simply, fragments not showing.

after calling commit(), expect see 3x3 board of sudoku_grid fragments, each containing 9 sudoku_cells. however, not shown

i have searched so, reread documentation, , searched more cannot understand why not showing.


i have tried:

when inflating fragment, inflate sudoku_cell layout instead, infact show 9 cell grid.

however each cell should grid containing 9 cells, leads me believe there may issue on sudoku_grid - sudoku_cell side, possibly sudoku_cell layout not being inflated correctly or being rooted correctly.

usually layoutinflator, , viewgroup passed allowing 1 inflate layout, in case of sudoku_cell, cannot find such method override, cause?


the code:

sudoku_cell.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               android:orientation="vertical"               android:layout_width="match_parent"               android:layout_height="match_parent">     <textview             android:id="@+id/celltext"             android:layout_width="35dp"             android:layout_margin="1px"             android:textsize="18dp"             android:layout_height="35dp"             android:textalignment="center"             android:textcolor="@color/clblack">     </textview> </linearlayout> 

sudokucell.java

public class sudokucell extends linearlayout{      private linearlayout layout;     private textview textview;     private context mcontext;     private point location;     private int index;      public sudokucell(context context) {         super(context);     }      public sudokucell(context context, attributeset attrs) {         super(context, attrs);         mcontext = context;          layoutinflater inflater = (layoutinflater) getcontext().getsystemservice(context.layout_inflater_service);         layout = (linearlayout) inflater.inflate(r.layout.sudoku_cell, this, true);          textview = layout.findviewbyid(r.id.celltext);         settext("");     }      public void setstatictext(string s){         if (textview != null) {             textview.settext(s);             textview.settypeface(textview.gettypeface(), typeface.bold);         }     }      public void settext(string s){         if (textview != null)             textview.settext(s);     }      public point getlocation() {         return location;     }      public void setlocation(point location) {         this.location = location;     }      public int getindex() {         return index;     }      public void setindex(int index) {         this.index = index;     } } 

9 of these sudoku_cell's added sudoku_grid shown below in oncreateview() , populategrid() methods:

sudoku_grid.xml

<?xml version="1.0" encoding="utf-8"?> <gridlayout         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margin="2px"         android:id="@+id/grid_layout"         android:columncount="3"         android:rowcount="3">  </gridlayout> 

sudokugrid.java

public class sudokugrid extends fragment{      private gridlayout gridlayout;     private list<gridfragmentlistener> listeners = new arraylist<>();     private context mactititycontext;     private boolean fragment_location_centre;     private float screen_dp;     private int grid_margins_dp = 1;     private int colorodd, coloreven;     private list<integer> presetgrid;     private view previousview;     private drawable previousviewdrawable;      public void addgridlistener(gridfragmentlistener gridfragmentlistener) {         listeners.add(gridfragmentlistener);     }      public void removegridlistener(gridfragmentlistener gridfragmentlistener) {         listeners.remove(gridfragmentlistener);     }      protected void notifyvaluechanged(view value) {         (gridfragmentlistener listener : listeners) listener.onvaluechange(value);     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         mactititycontext = getactivity();         view v = inflater.inflate(r.layout.sudoku_grid, container, false);          //set grid view         gridlayout = v.findviewbyid(r.id.grid_layout);          populategrid();         return v;     }      private void populategrid() {          (int = 0; < 9; i++) {             sudokucell sudokucell = new sudokucell(mactititycontext);             sudokucell.setbackgroundcolor(((i % 2) == 0) ? r.color.clodd : r.color.cleven);             system.out.printf("sudoku cell id [ index = " + string.valueof(i) + " ] - getid() = " + sudokucell.getid());             sudokucell.setlocation(getpoint(i));             sudokucell.setindex(i);             sudokucell.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view view) {                     notifyvaluechanged(view);                 }             });             sudokucell.setonfocuschangelistener(new view.onfocuschangelistener() {                 @override                 public void onfocuschange(view view, boolean b) {                     sudokucell cell = (sudokucell) view;                     if (b)                         view.setbackgroundcolor(contextcompat.getcolor(mactititycontext, r.color.clselected));                     else                     view.setbackgroundcolor(contextcompat.getcolor(mactititycontext, (cell.getindex() % 2 == 0) ? r.color.clodd : r.color.cleven));                 }             });             sudokucell.setstatictext(string.valueof(i));             gridlayout.addview(sudokucell, i);         }     }      private point getpoint(int i) {         int y = 0;         while (i > 2){             y++;             -= 3;         }         return new point(i, y);     }      @override     public void onstart() {         super.onstart();         try {             addgridlistener((gridfragmentlistener) getactivity());         } catch (classcastexception e) {             throw new classcastexception(                     getactivity().getclass().tostring()                             + " not implement detailsfragment.detailsfragmentlistener interface.");         }      }      @override     public void onstop() {         super.onstop();         removegridlistener((gridfragmentlistener) getactivity());     } } 

in main activity, handle creation of 9 sudoku_grid fragments placed each respective framelayout, form 3x3 matrix of sudoku_grids, form sudoku board

activity_main_sudoku.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout         xmlns:android="http://schemas.android.com/apk/res/android"         xmlns:tools="http://schemas.android.com/tools"         xmlns:app="http://schemas.android.com/apk/res-auto"         android:layout_width="match_parent"         android:layout_height="match_parent"         tools:context="wrap302.nmu.task1.mainactivitysudoku"         android:background="@color/cldarkgrey">      <linearlayout             android:orientation="vertical"             android:layout_width="match_parent"             android:layout_height="wrap_content" android:id="@+id/linearlayout2">         <textview                 android:layout_width="match_parent"                 android:layout_height="wrap_content"                 android:id="@+id/textview"                 android:text="@string/app_title"                 android:layout_margintop="5dp"                 android:layout_marginbottom="5dp"                 android:textcolor="@color/clwhite"                 android:textappearance="@style/textappearance.appcompat.display1"                 android:textalignment="center"                 android:textstyle="bold"                 android:fontfamily="sans-serif"/>         <textview                 android:text="@string/lblscore"                 android:textcolor="@color/clwhite"                 android:layout_width="match_parent"                 android:layout_height="wrap_content" android:id="@+id/lblscore"                 android:textappearance="@style/textappearance.appcompat.button" android:textalignment="center"                 android:layout_marginbottom="5dp"/>     </linearlayout>      <gridlayout             android:layout_gravity="center"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:columncount="3"             android:rowcount="3"             android:id="@+id/main_sudokugrid_container">         <framelayout                 android:layout_column="0"                 android:layout_row="0"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame00"/>         <framelayout                 android:layout_column="1"                 android:layout_row="0"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame01"/>         <framelayout                 android:layout_column="2"                 android:layout_row="0"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame02"/>         <framelayout                 android:layout_column="0"                 android:layout_row="1"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame10"/>         <framelayout                 android:layout_column="1"                 android:layout_row="1"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame11"/>         <framelayout                 android:layout_column="2"                 android:layout_row="1"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame12"/>         <framelayout                 android:layout_column="0"                 android:layout_row="2"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame20"/>         <framelayout                 android:layout_column="1"                 android:layout_row="2"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame21"/>         <framelayout                 android:layout_column="2"                 android:layout_row="2"                 android:layout_width="wrap_content"                 android:background="@color/clwhite"                 android:layout_height="wrap_content" android:id="@+id/frame22"/>     </gridlayout>      <linearlayout             android:orientation="horizontal"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_alignparentbottom="true"             android:gravity="center" android:id="@+id/linearlayout">         <gridlayout                 android:layout_width="wrap_content"                 android:layout_height="match_parent"                 android:paddingtop="0dp">             <button                     android:layout_row="0"                     android:layout_column="0"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn1"                     android:text="1"/>             <button                     android:layout_row="0"                     android:layout_column="1"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn2"                     android:text="2"/>             <button                     android:layout_row="0"                     android:layout_column="2"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn3"                     android:text="3"/>             <button                     android:layout_row="1"                     android:layout_column="0"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn4"                     android:text="4"/>             <button                     android:layout_row="1"                     android:layout_column="1"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn5"                     android:text="5"/>             <button                     android:layout_row="1"                     android:layout_column="2"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn6"                     android:text="6"/>             <button                     android:layout_row="2"                     android:layout_column="0"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn7"                     android:text="7"/>             <button                     android:layout_row="2"                     android:layout_column="1"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn8"                     android:text="8"/>             <button                     android:layout_row="2"                     android:layout_column="2"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:id="@+id/btn9"                     android:text="9"/>             <button                     android:layout_row="0"                     android:layout_column="3"                     android:layout_rowspan="3"                     android:layout_width="wrap_content"                     android:layout_height="wrap_content"                     android:layout_gravity="fill_vertical"                     android:id="@+id/btnclear"                     android:text="clear"/>         </gridlayout>     </linearlayout> </relativelayout> 

mainactivitysudoku.java

public class mainactivitysudoku extends appcompatactivity implements gridfragmentlistener {      private fragmentmanager fragmentmanager;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main_sudoku);         createsudokugrid();     }      private void createsudokugrid() {         fragmentmanager = getfragmentmanager();         fragmenttransaction fragmenttransactionmanager = fragmentmanager.begintransaction();          fragmenttransactionmanager.add(r.id.frame00, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame01, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame02, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame10, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame11, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame12, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame20, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame21, new sudokugrid());         fragmenttransactionmanager.add(r.id.frame22, new sudokugrid());          fragmenttransactionmanager.commit();      }      @override     protected void onstart() {         super.onstart();         toast.maketext(this, "activity started & viewable", toast.length_short).show();     }      // gridfragmentlistener     @override     public void onvaluechange(view view) {         //todo here received view     } } 

well, solution proved simpler expected.

in short, solution move sudokucell constructor code

sudokucell(context context, attributeset attrs) 

to

sudokucell(context context) 

since constructor calling.

a few other changes can made improvement, solves fragments not being displayed.

quite simple error


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -