java - App crashes when calling startActivityForResult -
i'm working on first android app, in case me cook better. have 2 activities i'm struggling with, mainactivity.java , whatcanimake.java. when start whatcanimake mainactivity using startactivity(intent)
, i'm able pass objects using parcelables , use whatcanimake. issue want able pass list of i've changed in whatcanimake activity. example: pass in list of ingredients have, , whatcanimake removes ingredients recipe uses. i'd pass updated list of remaining ingredients.
the problem i'm having startactivityforresult()
. able use startactivity()
, pass intent parcelables, , bob's uncle. when changed on startactivityforresult()
, new activity fails launch, app crashes before hitting oncreate()
. points implementation of startactivityforresult()
, i've read every stack overflow question related startactivityforresult()
, i'm here!
my code starting whatcanimake below, onactivityresult()
, corresponding code inside whatcanimake.
it's odd me can change out startactivityforresult()
startactivity()
, app works again! in advance.
mainactivity.java:
public void searchrecipe(view view) { crossviewvariables.setmakesort('a'); intent recipe = new intent(this, whatcanimake.class); recipe.putextra("pantry",stock); recipe.putextra("cookbook",activecookbook); recipe.setflags(intent.flag_activity_forward_result); startactivityforresult(recipe,r.integer.search_by_recipe); //startactivity(recipe); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch(requestcode) { case (r.integer.search_by_recipe)://comes recipe search? if (resultcode == activity.result_ok) { bundle b = data.getextras(); stock = b.getparcelable("pantry"); } if (resultcode == activity.result_canceled) { } case (r.integer.search_by_ingredient)://comes recipe search? if (resultcode == activity.result_ok) { bundle b = data.getextras(); stock = b.getparcelable("pantry"); } if (resultcode == activity.result_canceled) { } case (r.integer.make_recipe)://comes seerecipe? if (resultcode == activity.result_ok) { bundle b = data.getextras(); stock = b.getparcelable("pantry"); } if (resultcode == activity.result_canceled) { toast.maketext(getapplicationcontext(), "cancelled", toast.length_long).show(); } case (r.integer.edit_pantry): if (resultcode == activity.result_ok) { bundle b = data.getextras(); stock = b.getparcelable("pantry"); } } }
whatcanimake.java:
intent result = new intent(); result.putextra("pantry", stock); setresult(activity.result_ok,result); finish();
logcat:
fatal exception: main process: com.example.schre.mememachine, pid: 26721 java.lang.illegalstateexception: not execute method android:onclick @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:293) @ android.view.view.performclick(view.java:5697) @ android.widget.textview.performclick(textview.java:10826) @ android.view.view$performclick.run(view.java:22526) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5697) @ android.widget.textview.performclick(textview.java:10826) @ android.view.view$performclick.run(view.java:22526) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) caused by: android.util.androidruntimeexception: forward_result_flag used while requesting result @ android.app.instrumentation.checkstartactivityresult(instrumentation.java:1884) @ android.app.instrumentation.execstartactivity(instrumentation.java:1545) @ android.app.activity.startactivityforresult(activity.java:4283) @ android.support.v4.app.basefragmentactivityjb.startactivityforresult(basefragmentactivityjb.java:50) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:79) @ android.app.activity.startactivityforresult(activity.java:4230) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:859) @ com.example.schre.mememachine.mainactivity.searchrecipe(mainactivity.java:91) @ java.lang.reflect.method.invoke(native method) @ android.support.v7.app.appcompatviewinflater$declaredonclicklistener.onclick(appcompatviewinflater.java:288) @ android.view.view.performclick(view.java:5697) @ android.widget.textview.performclick(textview.java:10826) @ android.view.view$performclick.run(view.java:22526) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)
remove line:
recipe.setflags(intent.flag_activity_forward_result);
you not need startactivityforresult()
, , it's causing crash.
Comments
Post a Comment