调用startActivityForResult时,应用程序崩溃

时间:2017-08-14 21:09:56

标签: java android android-intent

我正在开发我的第一个Android应用程序,特别是在这种情况下可以帮助我做得更好。我有2个我正在努力的活动,我的MainActivity.java和whatCanIMake.java。当我使用startActivity(intent)从MainActivity开始whatCanIMake时,我能够使用parcelables传递对象,并且通常使用whatCanIMake。我的问题是我希望能够传回我在whatCanIMake活动中改变的所有内容的列表。例如:我传递了我目前拥有的所有成分的列表,然后whatCanIMake删除了食谱使用的成分。我想传回更新的剩余成分清单。

我遇到的问题是startActivityForResult()。以前我成功地使用startActivity(),用额外的parcelables传递意图,Bob是你的叔叔。当我切换到startActivityForResult()时,新活动无法启动,因为应用在点击onCreate()之前崩溃了。显然这指向我startActivityForResult()的实现,所以我基本上已经阅读了与startActivityForResult()相关的每个堆栈溢出问题,现在我就在这里!

我在下面启动whatCanIMake的代码,以及whatCanIMake中的onActivityResult()和相应代码。

对我而言,我可以简单地为startActivityForResult()更改startActivity()并且该应用再次有效!感谢您提前提供任何帮助。

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 from 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 from 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 from 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: Could not execute method for android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5697)
                                                                               at android.widget.TextView.performClick(TextView.java:10826)
                                                                               at android.view.View$PerformClick.run(View.java:22526)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:158)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5697) 
                                                                               at android.widget.TextView.performClick(TextView.java:10826) 
                                                                               at android.view.View$PerformClick.run(View.java:22526) 
                                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:158) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
                                                                            Caused by: android.util.AndroidRuntimeException: FORWARD_RESULT_FLAG used while also requesting a result
                                                                               at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1884)
                                                                               at android.app.Instrumentation.execStartActivity(Instrumentation.java:1545)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4283)
                                                                               at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
                                                                               at android.app.Activity.startActivityForResult(Activity.java:4230)
                                                                               at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
                                                                               at com.example.schre.mememachine.MainActivity.searchRecipe(MainActivity.java:91)
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                               at android.view.View.performClick(View.java:5697) 
                                                                               at android.widget.TextView.performClick(TextView.java:10826) 
                                                                               at android.view.View$PerformClick.run(View.java:22526) 
                                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:158) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7224) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

2 个答案:

答案 0 :(得分:1)

而不是:

startActivityForResult(recipe,R.integer.search_by_recipe);

试试这个:

startActivityForResult(recipe, getResources().getInteger(R.integer.search_by_recipe));

答案 1 :(得分:0)

删除此行:

recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);

startActivityForResult()你不需要它,而且它导致你的崩溃。