调用startActivityForResult时应用崩溃

调用startActivityForResult时应用崩溃

本文介绍了调用startActivityForResult时应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on my first Android app, specifically in this case something to help me cook better. I have 2 activities that I'm currently struggling with, my MainActivity.java and whatCanIMake.java. When I start whatCanIMake from MainActivity using startActivity(intent), I'm able to pass objects using parcelables and generally use whatCanIMake. My issue is that I want to be able to pass back a list of everything that I've changed in my whatCanIMake activity. For example: I pass in a list of all the ingredients I currently have, and then whatCanIMake removes the ingredients that a recipe uses. I'd like to pass back the updated list of remaining ingredients.

我遇到的问题是startActivityForResult().以前,我曾经能够成功使用startActivity(),并将附加包裹的意图传递给它,而鲍勃是你的叔叔.当我切换到startActivityForResult()时,新活动无法启动,因为应用程序在点击onCreate()之前崩溃了.显然,这指向了startActivityForResult()的实现,因此我基本上已经阅读了与startActivityForResult()相关的每个堆栈溢出问题,现在我在这里!

The problem I'm having is with startActivityForResult(). Previously I was successfully able to use startActivity(), pass it the intent with the extra parcelables, and Bob's your uncle. When I changed over to startActivityForResult(), the new activity fails to launch, as the app crashes before hitting onCreate(). Obviously this points to my implementation of startActivityForResult(), so I've read basically every stack overflow question related to startActivityForResult() and now I'm here!

下面是我启动whatCanIMake的代码,以及whatcanIMake中的onActivityResult()和相应代码.

My code for starting whatCanIMake is below, as well as the onActivityResult() and corresponding code inside whatCanIMake.

对我来说真是太奇怪了,我可以将startActivityForResult()换成startActivity(),然后该应用程序又可以正常工作了!感谢您的任何事先帮助.

It's just so odd to me that I can simply change out startActivityForResult() for startActivity() and the app works again! Thanks for any help in advance.

MainActivity.java:

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:

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()不需要它,这就是导致崩溃的原因.

You do not need it for startActivityForResult(), and it's what is causing your crash.

这篇关于调用startActivityForResult时应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:52