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

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" />

Related

Android App Crash After Several Image Taking

I am developing an application for Android which is supposed to take pictures from camera and upload those images to database. However, the app runs fine for first 8-10 pictures, but when I attempt to take more pictures the app gets crashed.
The error from log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.salesservice, PID: 18379
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.project.salesservice/com.project.salesservice.user.UserVisitDetailActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:5195)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8057)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:496)
at android.net.Uri$StringUri.<init>(Uri.java:486)
at android.net.Uri.parse(Uri.java:458)
at com.project.salesservice.user.UserVisitDetailActivity.getImageUri(UserVisitDetailActivity.java:582)
at com.project.salesservice.user.UserVisitDetailActivity.onActivityResult(UserVisitDetailActivity.java:558)
at android.app.Activity.dispatchActivityResult(Activity.java:8516)
at android.app.ActivityThread.deliverResults(ActivityThread.java:5188)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5236) 
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2187) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8057) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011) 
Code for starting intent
imageUri = null;
Intent takePictureIntent = new Intent();
takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
takePictureIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
On Activity Result Code
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == UserVisitDetailActivity.RESULT_OK && data != null){
System.out.println("Masuk Camera");
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageUri = getImageUri(getApplicationContext(), photo);
if(checkInOut.getText().equals("Check In")) {
System.out.println(imageUri);
checkInHolder.setVisibility(View.VISIBLE);
checkInHolder.setImageURI(imageUri);
} else if(checkInHolder.getVisibility() == View.VISIBLE && checkInOut.getText().equals("Check Out")) {
System.out.println(imageUri);
checkOutHolder.setVisibility(View.VISIBLE);
checkOutHolder.setImageURI(imageUri);
}
}
}
Get Image URI Code
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
The problem is solved when the photos stored in the gallery are deleted. And the app will start to crash again if the photos reach 10 in gallery. Sorry if my grammar is not correct.
Really appreciate for your help. Thank you
Maybe you can optimize your image taking. The lack of memory on the device may be one of the cause of this problem. If there is not enough memory, the program may crash since taking pictures uses up memory. You might think about compressing the pictures before submitting them to the database or putting in place a system that releases memory after each picture is taken to solve this problem.
You could also check if the call to the MediaStore.Images.Media.insertImage method returns an empty string which is then passed to the Uri.parse method causing a NullPointerException. You should check if the MediaStore.Images.Media.insertImage method actually returns a valid value. If it does not, you should consider using another method to insert the image into the internal storage.

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

App crashes when calling dispatchTakePictureIntent

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)

the application is crashing when i load an image

I'm following a tutorial to load some images using uri and picasso method, it works until you select the image and then the application crash.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.johan.portfolijs, PID: 13364
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:17998 flg=0x1 }} to activity {com.example.johan.portfolijs/com.example.johan.portfolijs.MainActivity}: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to com.squareup.picasso.Target
at android.app.ActivityThread.deliverResults(ActivityThread.java:4605)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
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:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
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:964)
Caused by: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to com.squareup.picasso.Target
at com.example.johan.portfolijs.MainActivity.onActivityResult(MainActivity.java:87)
at android.app.Activity.dispatchActivityResult(Activity.java:7759)
Here you are getting the class cast exception while assigning it to URI, so check the code again .
Try this,
data.getData().getPath()
or
Uri selectedImageURI = data.getData()
Adding this first conditional should work:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

IllegalStateException: Can not perform this action after onSaveInstanceState with onActivityResult

I am new to Android development. I got an issue. I tried the last couple of hour but I can't figure out this. if so I got a popular question. IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager but failed because of the lack of Android development experience.
Here is code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
Here is the error:
01-04 05:08:57.010 13609-13609/com.nazmul.aznazgame.bitlife
D/AndroidRuntime: Shutting down VM 01-04 05:08:57.068
13609-13609/com.nazmul.aznazgame.bitlife E/AndroidRuntime: FATAL
EXCEPTION: main
Process: com.nazmul.aznazgame.bitlife, PID: 13609
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=4, result=-1, data=Intent { (has extras)
}} to activity
{com.nazmul.aznazgame.bitlife/com.nazmul.aznazgame.bitlife.MainActivity}:
java.lang.IllegalStateException: Can not perform this action after
onSaveInstanceState
at android.app.ActivityThread.deliverResults(ActivityThread.java:3574)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352)
at android.os.Handler.dispatchMessage(Handler.java:102)
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.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:2053)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2079)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:678)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:632)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:143)
at com.nazmul.aznazgame.bitlife.MainActivity.showEducationDialog(MainActivity.java:1716)
at com.nazmul.aznazgame.bitlife.MainActivity.manageActivity(MainActivity.java:604)
at com.nazmul.aznazgame.bitlife.MainActivity.onActivityResult(MainActivity.java:580)
at android.app.Activity.dispatchActivityResult(Activity.java:6192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3570)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3617) 
at android.app.ActivityThread.access$1300(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1352) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
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)  01-04
05:08:59.825 13609-13651/com.nazmul.aznazgame.bitlife
I/CrashlyticsCore: Crashlytics report upload complete:
5C2EEA4F018E-0001-3529-63E978D09744 01-04 05:08:59.955
13609-13609/com.nazmul.aznazgame.bitlife I/Process: Sending signal.
PID: 13609 SIG: 9
You must call super.onActivityResult(requestCode, resultCode, data) before doing any FragmentTransactions in your onActivityResult() method as that call is what 'unlocks' the FragmentManager and notes that you are in a valid state to do FragmentTransactions.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Add this line
super.onActivityResult(requestCode, resultCode, data);
// This all remains the same
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
MyCustomDialogFragment newPopup = new MyCustomDialogFragment();
newPopup.setMyClickListener(MainActivity.this);
FragmentManager fragmentManager = getSupportFragmentManager();
newPopup.show(fragmentManager, "CashReceivePopup");
}
}
}
It is a common problem. You have 2 options how to deal with this issue.
you can override the show() method and put its content inside a try-catch block
You display the dialog in the followong way
FragmentManager fm = getSupportFragmentManager();
MyDialog d = new MyDialog();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(d, "dialog");
ft.commitAllowingStateLoss();

Categories

Resources