I wanted to make two buttons on my main activity to startActivityForResult
Every time I press either of these buttons my app force closes , and I can't figure out why!
Here is the code of the two buttons :
public void billiards (View v)
{
Intent billiards = new Intent(this,Billiards.class);
startActivityForResult(billiards,99710);
}
public void snooker (View v)
{
Intent snooker = new Intent(this,Snooker.class);
startActivityForResult(snooker,99711);
}
Here is the code of Billiards activity :
public void bsave (View v)
{
Double dbgames = Double.parseDouble(ebgames.getText().toString());
Double calcbgames = (dbgames)*0.50;
Double btotal = (calcbgames);
Intent billiards=new Intent();
billiards.putExtra("btot",String.valueOf(btotal));
setResult(99710,billiards);
finish();
}
Here is the code of Snooker Activity :
public void ssave (View v)
{
Double dsgames = Double.parseDouble(esgames.getText().toString());
Double calcsgames = (dsgames)*1.00;
Double stotal = (calcsgames);
Intent snooker=new Intent();
snooker.putExtra("stot",String.valueOf(stotal));
setResult(99710,snooker);
finish();
}
And here is the code of onActivityResult in my MainActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==99710) {
if (resultCode == RESULT_OK) {
String billiardstot = data.getStringExtra("btot");
String bt = billiardstot;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(BToatal, bt);
editor.commit();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Wrong Request Code", Toast.LENGTH_LONG).show();
}
}
if (requestCode==99711) {
if (resultCode == RESULT_OK) {
String snookertot = data.getStringExtra("stot");
String st = snookertot;
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(STotal, st);
editor.commit();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Wrong Result Code", Toast.LENGTH_LONG).show();
}
}
}
Whenever i press on Billiards or Snooker Buttons the app force closes
And here is the LogCat :
08-18 02:11:01.220 11972-11972/cafe.al_sheikhabuhamzehcafe E/AndroidRuntime: FATAL EXCEPTION: main
Process: cafe.al_sheikhabuhamzehcafe, PID: 11972
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at android.support.v4.app.BaseFragmentActivityEclair.checkForValidRequestCode(BaseFragmentActivityEclair.java:64)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:868)
at cafe.al_sheikhabuhamzehcafe.ItemMenu.billiards(ItemMenu.java:149)
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:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-18 02:11:03.043 11972-11972/cafe.al_sheikhabuhamzehcafe I/Process: Sending signal. PID: 11972 SIG: 9
java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
the values you are using for your requestCode are too large
stick to something less than 65535
Related
What is causing this null pointer exception when I try and get the String I set as an extra? I've read a lot of the other questions on here and can't work it out.
I'm passing a String to the intent, which I got from an enum using the .name() method in the onOKButtonClicked method:
public void onSquareButtonClicked(View v)
{
brushStyle = Paint.Cap.SQUARE;
TextView currentBrushText = (TextView)findViewById(R.id.currentBrush);
currentBrushText.setText("SQUARE");
}
public void onRoundButtonClicked(View v){
brushStyle = Paint.Cap.ROUND;
TextView currentBrushText = (TextView)findViewById(R.id.currentBrush);
currentBrushText.setText("ROUND");
}
public void onOkButtonClicked(View v){
Intent intent = new Intent();
String stringBrushStyle = brushStyle.name();
intent.putExtra("new brush", stringBrushStyle);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
And then trying to get the result back out here:
protected void onActivityResult(int requestCode, int resultCode,
#Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FingerPainterView myFingerPainter = (FingerPainterView)findViewById(R.id.fingerPainterView);
if(resultCode != RESULT_CANCELED) {
if (requestCode == COLOR_REQUEST_CODE ) {
int currentColor = data.getIntExtra("new color", 5);
myFingerPainter.setColour(currentColor);
} else if (requestCode == BRUSH_REQUEST_CODE) {
String currentBrush = getIntent().getExtras().getString("new brush");
myFingerPainter.setBrush(Paint.Cap.valueOf(currentBrush));
}
}
}
}
My Logcat - not sure what who=null means here, but I'm guessing that's what is causing the problem:
Process: com.example.myapplication, PID: 17757
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
at com.example.myapplication.MainActivity.onActivityResult(MainActivity.java:90)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
change the following line
String currentBrush = getIntent().getExtras().getString("new brush");
to
String currentBrush = data.getStringExtra("new brush");
My app is crashing when I click on the following button to use dispatchTakePictureIntent
My code:
ic_img_item.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dispatchTakePictureIntent(PHOTO);
}
});
private void dispatchTakePictureIntent(int requestCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, PHOTO_TEMP);
takePictureIntent.putExtra("return-data", true);
startActivityForResult(takePictureIntent, requestCode);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
try {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 2;
Bitmap bitmap = RotateBitmap(BitmapFactory.decodeStream(activity.getContentResolver().openInputStream(PHOTO_TEMP), null, bmOptions), getPictureOrientation(PHOTO_TEMP));
thumb = getThumbnail(bitmap);
setThumbnail(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Log:
--------- beginning of crash
2020-09-10 17:41:31.769 25093-25093/ubisolutions.net.datacenterinventory.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: ubisolutions.net.datacenterinventory.debug, PID: 25093
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 3087012 bytes
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3782)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: android.os.TransactionTooLargeException: data parcel size 3087012 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:615)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3653)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3774)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
2020-09-10 17:41:31.775 1239-1239/? E/NotificationService: Suppressing notification from package by user request.
2020-09-10 17:41:31.775 1239-25274/? I/QCALOG: [MessageQ_Client] connecting to server [/data/misc/location/mq/location-mq-s]
2020-09-10 17:41:31.776 1239-25274/? E/QCALOG: [MessageQ_Client] connect error: 111, [Connection refused]
I have seen some responses about similar issues but none of them helped me to solve it.
thanks.
Your code line
takePictureIntent.putExtra("return-data", true);
requests, that the picture data is returned directly, but it is too large to be passed back in the result. You should instead get it from PHOTO_TMP you are passing in the intent too.
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, PHOTO_TEMP)
I am new to Android Studio and trying to create a simple app. The app keeps crashing, when I need to save the data from the other activities, using onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
Job updatedJobDetails = (Job)data.getSerializableExtra("updatedJobDetails");
int position = data.getIntExtra("pos", -1);
adapter.remove(adapter.getItem(position));
adapter.insert(updatedJobDetails, position);
}
}
}
First it says, that I need to
onActivityResult should call super.onActivityResult
but when I do that, it crashes as soon as I use the method. Logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.smapassignment01, PID: 6809
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.smapassignment01/com.example.smapassignment01.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1
at android.app.ActivityThread.deliverResults(ActivityThread.java:4360)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:393)
at com.example.smapassignment01.MainActivity.onActivityResult(MainActivity.java:122)
at android.app.Activity.dispatchActivityResult(Activity.java:7454)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 6809 SIG: 9
Process 6809 terminated.
I can't really read from the logcat-report, what I've done wrong.
According to this SO-question it is because of getAdapterPosition - but I don't even use it, so I'm completely lost and have now used several hours, stuck.
UPDATE:
Just to show, that my pos has been sat in the other activity, from where the app crashes:
btnSaveNote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
job.setUserNotes(etJobNotes.getText().toString());
if (!cbAppliedJob.isChecked()){
job.setJobApplied(false);
}
else{
job.setJobApplied(true);
}
Intent data = new Intent();
position = getIntent().getIntExtra("pos", -1);
data.putExtra("pos", position);
data.putExtra("updatedJobDetails", job);
setResult(RESULT_OK, data);
finish();
}
});
I'm a novice coder and I've been trying to learn how to code for school in a few days and one thing I like to do is try and deconstruct other code to learn it better. So I was looking at trying to make a contact picker and I found a tutorial online, however, the code does not work, and I don't really understand what the error message means.
My code:
public class ContactActivity extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85500;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
}
public void pickContact(View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("MainActivity", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
* #param data
*/
private void contactPicked(Intent data) {
Button b1 = findViewById(R.id.button);
Button b2 = findViewById(R.id.button2);
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
b1.setText(name);
b2.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Error Message:
08-14 16:25:16.437 3923-3923/com.example.jackson.safetynet E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jackson.safetynet, PID: 3923
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode
at android.support.v4.app.BaseFragmentActivityApi14.checkForValidRequestCode(BaseFragmentActivityApi14.java:79)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
at com.example.jackson.safetynet.ContactActivity.pickContact(ContactActivity.java:29)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
So I see the line " Caused by: java.lang.IllegalArgumentException: Can only use lower 16 bits for requestCode " and I think that this is the problem. Is there something with the Result_Pick_Contact or what?
Layout Visual
Try using a smaller positive RESULT_PICK_CONTACT value
private static final int RESULT_PICK_CONTACT = 24;
As specified by this answer
You need to pass a positive number to startActivityForResult.
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.
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!
My code for starting whatCanIMake is below, as well as the onActivityResult() and corresponding code inside whatCanIMake.
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:
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)
Remove this line:
recipe.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
You do not need it for startActivityForResult(), and it's what is causing your crash.
Instead of:
startActivityForResult(recipe,R.integer.search_by_recipe);
Try this:
startActivityForResult(recipe, getResources().getInteger(R.integer.search_by_recipe));