2017-08-14 24 views
0

私は最初のAndroidアプリを開発しています。 MainActivity.javaとwhatCanIMake.javaという2つのアクティビティがあります。私がMainActivityからwhatCanIMakeをstartActivity(intent)を使って起動すると、私はパーセルブルを使ってオブジェクトを渡すことができ、一般的にwhatCanIMakeを使用することができます。私の問題は、whatCanIMakeアクティビティで変更したすべてのリストを返すことができるようにすることです。たとえば:私は現在持っているすべての成分のリストを渡し、次にwhatCanIMakeはレシピが使用する成分を取り除きます。更新された残りの成分リストを返信したいと思います。startActivityForResultを呼び出すとアプリケーションがクラッシュする

問題はstartActivityForResult()です。以前は、startActivity()を使用して、余分なパーセルブルとインテントを渡し、Bobさんはあなたの叔父をうまく利用することができました。 startActivityForResult()に変更したとき、新しいアクティビティは起動しません。これは、アプリがクラッシュしてから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(); 

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) 
+0

LogCatを使用して、クラッシュに関連するJavaスタックトレースを調べます。https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

フィルタを変更しました。設定、ログを見つけました。私はそれらを含めるために私の質問を編集します!ありがとう! – AssaultPotato

答えて

0

は、この行を削除します。

recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT); 

あなたがstartActivityForResult()のためにそれを必要としない、それはあなたのクラッシュを引き起こしているものです。

+0

私はちょうど同じ結論に達しました、私はそれを削除し、それは働いた!私はちょうど私がそれらをすべて修正するために私の活動の呼び出しの残りの部分を更新することを確認する必要があります。ありがとう、トン! – AssaultPotato

1

の代わりに:

startActivityForResult(recipe,R.integer.search_by_recipe); 

はこれを試してみてください:

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

私はそれを行けば、私は戻って報告します。提案していただきありがとうございます! – AssaultPotato

+0

何も助けてくれていないようです – AssaultPotato

関連する問題