App crashes when calling startActivityForResult - java

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));

Related

onActivityResult should call super.onActivityResult

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();
}
});

How Do I Create A Contact Picker Activity?

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.

Android Studio Handover variables between activities

I´ve got one task. I have to put the points of a game from GameActivity to the GameOverActivity. I tried for hours, but it doesn´t work... Here is my try:
This is in my GameActivity (put an extra):
public void finishGameActivity(){
Intent i = getIntent();
i.putExtra("key1", points);
setResult(Activity.RESULT_OK, i);
finish();
}
And this is my GameOverActivity:
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("key1");
TextView points_stats = (TextView) findViewById(R.id.point_stats);
points_stats.setText(value);
The Application crashes. This is the error code:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.chralt.circlesafe, PID: 6849
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.chralt.circlesafe/de.chralt.circlesafe.GameOverActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2691)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6120)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
at de.chralt.circlesafe.GameOverActivity.onCreate(GameOverActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2644)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2752) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1461) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6120) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Where are you trying to extract the points in your GameOverActivity? Since you are calling "SetResult" it should be inside your GameOverActivity's onActivityResult method. If this is the case I believe it should look something like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the GameActivity.
int value = data.getIntExtra("key1", some_default_value);
}
}

How to open a file from a URI/Uri in Android

I am trying to open a file from my emulator's internal storage (was trying to put it in sdcard but it put to internal storage?). I have the user choose a file, then pass that file's Uri (note the case) to a text editing activity via intent. In this activity, I've tried a few things. My current code looks like this (excuse the madness, I've been trying a LOT of things):
try {
Intent intent = getIntent();
File file;
Uri uri = Parcels.unwrap(intent.getParcelableExtra("uri"));
URI newUri = new URI("file://"+uri.toString());
Log.d("uri", uri.toString());
Log.d("newUri", newUri.toString());
file = new File(newUri);
if (intent != null) {
Log.d("uriPath", uri.toString());
if (uri != null) {
file = new File(uri.getPath());
String text = readTextFile(file);
if (text != null) {
Log.d("in if text not null", text);
mEditText.setText(text);
}
// mEditText.setText(readTextFile(file));
}
}
}
catch(URISyntaxException e) { e.printStackTrace(); }
I've tried a LOT of things, with no success. I'm either getting file not found (as it's not prefixing the file with anything, just trying to load "document/primary%3Aindex.html"), or other exceptions, currently:
06-06 05:43:18.826 10794-10794/net.a40two.pext E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.a40two.pext, PID: 10794
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.a40two.pext/net.a40two.pext.ui.EditorActivity}: java.lang.IllegalArgumentException: Found authority in URI: file://content://com.android.externalstorage.documents/document/primary%3Aindex.html
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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: Found authority in URI: file://content://com.android.externalstorage.documents/document/primary%3Aindex.html
at java.io.File.checkURI(File.java:230)
at java.io.File.<init>(File.java:175)
at net.a40two.pext.ui.EditorActivity.onCreate(EditorActivity.java:67)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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) 
This happens even without the file:// concatenation. Most anything I can find online is either incredibly old, doesn't work, or both. Any help is sincerely appreciated.
The code in my main activity where I'm getting this URI is straight from developers.android.com:
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
// The ACTION_OPEN_DOCUMENT intent was sent with the request code
// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
// response to some other intent, and the code below shouldn't run at all.
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// The document selected by the user won't be returned in the intent.
// Instead, a URI to that document will be contained in the return intent
// provided to this method as a parameter.
// Pull that URI using resultData.getData().
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
Log.i("onActivityResult", "Uri: " + uri.toString());
Intent intent = new Intent(MainActivity.this, EditorActivity.class);
intent.putExtra("uri", Parcels.wrap(uri));
startActivity(intent);
finish();
}
}
}

startActivityForResult force closes my app

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‬

Categories

Resources