android Nullpointerexception with intent data=null always -


i creating app, contains 3 activities, say, a, b , c. activity main activity, launches when app opens.

activity called activity b using startactivityforresult(intent, 1); activity b returns result, captured, , within onactivityresult of activity a, launching activity c using startactivityforresult(intent2, 2);

here onactivityresult activity a

@override protected void onactivityresult(int requestcode, int resultcode, intent data){     log.d("main","inside on activity result requestcode : " + requestcode);     if(requestcode == 1){         if(resultcode == result_ok){             log.d("mainact", "inside result code ok");             if(data.getstringextra("puzzleid")!=null){                 log.d("mainact", "inside puzzle has been selected");                 string puzzleidresult = data.getstringextra("puzzleid");                 intent puzzleintent = new intent(mainactivity.this, trialactivity.class);                 puzzleintent.putextra("puzzleid", puzzleidresult);                 startactivityforresult(puzzleintent, 2);             }          }     }else if(requestcode == 2){          log.d("mainact", "inside request code 2 result code : " + resultcode);          if(resultcode == 0){              if(data!=null){              log.d("mainact", "inside status not null");              string puzzlestatus = data.getstringextra("status");              log.d("mainact", "puzzle status retrieved : " + puzzlestatus);              if(puzzlestatus.equals("pause")){                  log.d("mainact", "inside puzzle status paused");              }     }else{           log.d("mainact", "data null!!");     } } 

this return result intent activity c

@override protected void onpause() {     super.onpause();     log.d(trialtag, "on pause called");         log.d(trialtag, "sending intent");         string puzzleid = integer.tostring(puzzle_id);     intent returnpause = new intent();     returnpause.putextra("status", "pause");     returnpause.putextra("puzzleid", puzzleid);     if(getparent() == null){         setresult(activity.result_ok, returnpause);     }else{         getparent().setresult(activity.result_ok, returnpause);     }     finish();  } 

on returning activity c, getting nullpointerexception, , statement.

java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=2, result=0, data=null} activity  {com.mypackagename.sudoku/com.mypackagename.sudoku.mainactivity}: java.lang.nullpointerexception 

i put in data!=null check later prevent crash. data null!! everytime pause in activity c. tried getparent method after reading through other solutions on so, not helping. why data getting set in activity b not in activity c?this first full fledged app, please excuse ignorance.

it seems setresult() method not getting called since result code set per exception 0 (result_canceled) , not -1 (result_ok)

are sure activity c not crashing @ integer.tostring(puzzle_id)??

debugging onpause() method of activity c resolve crash.

p.s: that's why should avoid using magic numbers if(resultcode == 0)

p.s: fyi, result_canceled (0) : if child activity fails reason (such crashing), parent activity receive result code result_canceled.

update

setting result in onpause() or ondestroy wont work. set result in either onbackpressed() or in onfinish()

see result not set in onpause() using setresult() when pressing button


Comments

  1. To distinguish between games of skill and world777 games of chance, Indian courts use the dominating factor or predominance test. It ultimately boils down to whether aspect — skill or chance — is the most important in determining the game's outcome.

    ReplyDelete

Post a Comment

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -