gpt4 book ai didi

java - 调用 startActivityForResult 时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 12:22:15 24 4
gpt4 key购买 nike

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

我遇到的问题是 startActivityForResult()。以前,我成功地使用了 startActivity(),将 Intent 与额外的 Parcelables 一起传递,而 Bob 是你的叔叔。当我切换到 startActivityForResult() 时,新 Activity 无法启动,因为应用程序在点击 onCreate() 之前崩溃了。显然,这指向我的 startActivityForResult() 实现,因此我基本上已经阅读了与 startActivityForResult() 相关的所有堆栈溢出问题,现在我在这里!

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

这对我来说太奇怪了,我只需将 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();

日志猫:

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

关于java - 调用 startActivityForResult 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com