android - Fragment not added to activity when method is called -


i have main activity called chessboard. within chessboard, in oncreate, set fragment activity gameinfofragment. after this, call made query sqlite database, after method in class sqlitedatainterpreter called. here, call made method in gameinfofragment class, imageviews in fragment set in chessboard's oncreate should populated.

when attempt start chessboard activity, receive following error:

caused by: java.lang.illegalstateexception: fragment gameinfofragment{2ef389d9 id=0x7f09008d infotest} not attached activity 

the code in chessboard's oncreate following:

fragmentmanager manager = this.getfragmentmanager();         fragmenttransaction transaction = manager.begintransaction();          if (chessboard.movecounter % 2 == 0) {             transaction.add(com.zlaporta.chessgame.r.id.game_play_screen, gameinfo, "infotest");          } else {             transaction.add(com.zlaporta.chessgame.r.id.game_play_black_screen, gameinfo, "infotestblack");         }         transaction.commit();          toolbar toolbar = (toolbar) findviewbyid(r.id.app_bar);         toolbar.settitle("current game");         setsupportactionbar(toolbar);           dbadapter boardstatedb = new dbadapter(chessboard.this);         boardstatedb.open();           cursor cur = boardstatedb.getallrows();          if (cur != null && cur.movetofirst()) {             cur.movetolast();             string piecedata = cur.getstring(cur.getcolumnindex("piecepositions"));             whitekingmoved = cur.getint(cur.getcolumnindex("whitekingmoved"));             blackkingmoved = cur.getint(cur.getcolumnindex("blackkingmoved"));             leftwrmoved = cur.getint(cur.getcolumnindex("whiterook1moved"));             rightwrmoved = cur.getint(cur.getcolumnindex("whiterook2moved"));             leftbrmoved = cur.getint(cur.getcolumnindex("blackrook1moved"));             rightbrmoved = cur.getint(cur.getcolumnindex("blackrook2moved"));             movecounter = cur.getint(cur.getcolumnindex("movecount"));             string mademoves = cur.getstring(cur.getcolumnindex("mademoves"));             storedtakenpieces = cur.getstring(cur.getcolumnindex("takenpieces"));             sqlitedatainterpreter boardrefresher = new sqlitedatainterpreter();             boardrefresher.refreshboard(piecedata, mademoves, chessboard.this, storedtakenpieces);         } else {             boardstatedb.updategamestate("r00 k01 b02 q03 a04 b05 k06 r07 p10 p11 p12 p13 p14 p15 p16 p17 p60 p61 p62 p63 p64 p65 p66 p67 r70 k71 b72 q73 a74 b75 k76 r77",                     0, 0, 0, 0, 0, 0, 0, " ", " ");             cursor curs = boardstatedb.getallrows();             curs.movetolast();              string piecedata = curs.getstring(curs.getcolumnindex("piecepositions"));             whitekingmoved = curs.getint(curs.getcolumnindex("whitekingmoved"));             blackkingmoved = curs.getint(curs.getcolumnindex("blackkingmoved"));             leftwrmoved = curs.getint(curs.getcolumnindex("whiterook1moved"));             rightwrmoved = curs.getint(curs.getcolumnindex("whiterook2moved"));             leftbrmoved = curs.getint(curs.getcolumnindex("blackrook1moved"));             rightbrmoved = curs.getint(curs.getcolumnindex("blackrook2moved"));             movecounter = curs.getint(curs.getcolumnindex("movecount"));             string mademoves = curs.getstring(curs.getcolumnindex("mademoves"));             storedtakenpieces = curs.getstring(curs.getcolumnindex("takenpieces"));             sqlitedatainterpreter boardrefresher = new sqlitedatainterpreter();             boardrefresher.refreshboard(piecedata, mademoves, chessboard.this, storedtakenpieces); 

and relevant code in gameinfofragment class causing problems:

public void settakenpiece(string[] takenpiecerecord) {         takenpiece = takenpiecerecord;          int c;         (c = 0; c < takenpiece.length; c++) {             string takenpieceholder = "t" + c;             string piececheck = takenpiece[c];             //system.out.println(square);                  int takenpieceholderid = this.getresources().getidentifier(takenpieceholder, "id", getactivity().getpackagename());                 imageview takenpieceholdersquare = (imageview) v.findviewbyid(takenpieceholderid); 

the main line of code causing issues try set value of takenpieceholderid in code directly above.

how can ensure fragment added imageviews populated upon start of chessboard activity?

you should check if activity exists , if isadded() true

public void settakenpiece(string[] takenpiecerecord) {         activity = getactivity();         if (isadded() && activity) {             takenpiece = takenpiecerecord;              int c;             (c = 0; c < takenpiece.length; c++) {                 string takenpieceholder = "t" + c;                 string piececheck = takenpiece[c];                 //system.out.println(square);                  int takenpieceholderid = this.getresources().getidentifier(takenpieceholder, "id", getactivity().getpackagename());                 imageview takenpieceholdersquare = (imageview) v.findviewbyid(takenpieceholderid);      }} 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -