App crashes when calling dispatchTakePictureIntent - java

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)

Related

Null pointer exception in Android - onActivityResult()

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

How to upload multiple images to firebase in android?

I know this question has been asked here and answered but for some reasons, provided solutions are not working for me.
So here is how I'm trying to do this. I have two buttons in my layout one to open gallery and other to upload the images.
Defined members
int SELECT_PICTURES = 1;
ArrayList<Uri> mArrayUri = new ArrayList<Uri>();
Uri imageUri;
int up = 0;
int k =0;
Gallery button code
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURES);
On Activity result code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SELECT_PICTURES) {
if (resultCode == MainActivity.RESULT_OK) {
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
Log.i("count", String.valueOf(count));
int currentItem = 0;
while (currentItem < count) {
imageUri = data.getClipData().getItemAt(currentItem).getUri();
Log.i("uri", imageUri.toString());
mArrayUri.add(imageUri);
currentItem = currentItem + 1;
}
Log.i("listsize", String.valueOf(mArrayUri.size()));
} else if (data.getData() != null) {
String imagePath = data.getData().getPath();
}
}
}
}
Upload button code
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");
while (up < mArrayUri.size()){
// error is pointing to this line, line 82
filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();
up++;
k++;
}
});
}
}
The above code uploads one image and then crashes with the following error
01-24 12:37:37.416 8336-8336/com.codenemesis.uploading E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.codenemesis.uploading, PID: 8336
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:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
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:5610) 
at android.view.View$PerformClick.run(View.java:22265) 
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:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
Caused by: java.util.concurrent.RejectedExecutionException: Task com.google.firebase.storage.zzs#c59b7fe rejected from java.util.concurrent.ThreadPoolExecutor#4b6eb5f[Running, pool size = 2, active threads = 2, queued tasks = 128, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2049)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:814)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1360)
at com.google.firebase.storage.zzu.zzt(Unknown Source)
at com.google.firebase.storage.UploadTask.schedule(Unknown Source)
at com.google.firebase.storage.StorageTask.zzcls(Unknown Source)
at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
at **com.codenemesis.uploading.TestingActivity.up(TestingActivity.java:82)**
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:5610) 
at android.view.View$PerformClick.run(View.java:22265) 
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:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
Found what was causing the problem. I was incrementing the variables "up" and "k" within the onSuccessListener. That might be changing their values before uploading the URI, so I incremented them outside the onSuccessListener and it worked perfectly fine.
Upload button code
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("gpic");
while (up < mArrayUri.size()){
filepath.child(mArrayUri.get(k).getLastPathSegment()).putFile(mArrayUri.get(k)).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadURL = taskSnapshot.getDownloadUrl();
Toast.makeText(TestingActivity.this, downloadURL.toString(), Toast.LENGTH_SHORT).show();
}
});
up++;
k++;
}
}
Completable futures is much better approach for this problem.
Below upload method returns a completable future.
fun uploadImage(
path: String, imageUrl: Uri): CompletableFuture<String> {
val productImagesRef = storage.reference.child("$path/${imageUrl.lastPathSegment}")
val uploadTask = productImagesRef.putFile(imageUrl)
val promise = CompletableFuture<String>()
uploadTask.addOnFailureListener {
promise.completeExceptionally(it)
}.addOnSuccessListener {
productImagesRef.downloadUrl.addOnSuccessListener {
promise.complete(it.toString())
}.addOnFailureListener {
promise.completeExceptionally(it)
}
}
return promise
}
The caller can wait on multiple upload completable futures as below. Further, in android it would be ideal for the caller to spawn a new thread for multiple uploads to not block the UI main thread.
Thread {
val imageUploadPromises = imageUris.map {
uploadImage(product.group, product.type, product.name, it)
}
// wait for all uploads to finish. join() makes it wait.
CompletableFuture.allOf(*imageUploadPromises.toTypedArray()).join()
//To get individual downloadUrls
val downloadUrls = imageUploadPromises.map { it.get() }
}.start()

Android get file path of image with in Fragment [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I have an app and I need to take photos and then upload it to firebase. I basically need Uri path of the photo taken.
I read Get file path of image on Android, but I have a problem with NullPointerException.
Main difference between my problem and the problem above is that I'm doing it in a Fragment.
Here is my code:
mTakePhotoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
I get NullPointerException in line with Uri imgUri.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
mImgView.setImageBitmap(photo);
Uri imgUri = getImageUri(getActivity().getApplicationContext(), photo);
mManager.setPhotoUri(imgUri);
}
}
And here is the method I use to get Uri.
private Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
The exact error message is:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=android:fragment:1, request=22222, result=-1,
data=Intent { act=inline-data (has extras) }} to activity
{.MainActivity}:
java.lang.NullPointerException: uriString
I would appreciate any help.
The entire error log:
09-14 12:06:18.448 16497-16497/com.example.radzik.recipes E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.radzik.recipes, PID: 16497
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:1, request=22222, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.radzik.recipes/com.example.radzik.recipes.activity.MainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
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.NullPointerException: uriString
at android.net.Uri$StringUri.(Uri.java:475)
at android.net.Uri$StringUri.(Uri.java)
at android.net.Uri.parse(Uri.java:437)
at com.example.radzik.recipes.fragment.ChoosePhotoFragment.getImageUri(ChoosePhotoFragment.java:144)
at com.example.radzik.recipes.fragment.ChoosePhotoFragment.onActivityResult(ChoosePhotoFragment.java:135)
at android.app.Activity.dispatchActivityResult(Activity.java:6452)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
at android.app.ActivityThread.-wrap16(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
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) 
Your problem is with getActivity() becasue you are getting the intent of the current activity you were on which will fail.
So if you move your code to the OnActivityResult this will make it work.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//first call super
super.onActivityResult(requestCode, resultCode, data);
//rest of your code
}
Don't forget to add
<uses-permission android:name="android.permission.CAMERA" />
and/or without depends on your app
<uses-feature android:name="android.hardware.camera" />

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