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

我遇到的问题是startActivityForResult()。以前,我曾经能够成功使用startActivity(),并将其意图与多余的包裹一起传递给Bob,而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)

最佳答案

删除此行:

recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);


startActivityForResult()不需要它,这就是导致崩溃的原因。

10-08 17:23