I am trying to send message from my SecondActivity to FirstActivity to be printed. I am using startActivityForResult(intent, requestcode) to start the SecondActivity and then enter text and send the entered text to be displayed by FirstActivity using onActivityResult(). When I run the Activity and click on send I am getting error stating "println needs a message". The result is not getting delivered to the FirstActivity.
Below is the code:
FirstActivity:
private void StartSecondActivity() {
Log.i(tag, "SecondActivity");
Intent iSecond = new Intent(this, SecondActivity.class);
startActivityForResult(iSecond, REQUEST_CODE);
}
SecondActivity:
private void eClicked() {
Log.i(tag, "Clicked()");
String mes = mEditText.getText().toString();
Intent intent = new Intent();
intent.putExtra(EXTRA_MESSAGE, mes);
setResult(RESULT_OK, intent);
Log.i(tag, mes);
finish();
}
Here it does log correct message.
FirstActivity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(tag, "onActivityResult()");
if(requestCode == REQUEST_CODE ){
if(resultCode == RESULT_OK){
Intent inte = getIntent();
String m = inte.getStringExtra(SecondActivity.EXTRA_MESSAGE);
mTextView.setText(m);
}
}
}
It enters the onActivityResult and logs the message and then crashes with the below error message.
Error Message:
02-09 18:52:42.010: E/AndroidRuntime(1831): FATAL EXCEPTION: main
02-09 18:52:42.010: E/AndroidRuntime(1831): Process: droid.intentexp.intents, PID: 1831
02-09 18:52:42.010: E/AndroidRuntime(1831): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {droid.intentexp.intents/droid.intentexp.intents.FirstActivity}: java.lang.NullPointerException: println needs a message
I am not sure what is going wrong. Can someone please shed some light.
Thanks.
You should use data intent NOT getIntent()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(tag, "onActivityResult()");
if (requestCode == REQUEST_CODE ) {
if (resultCode == RESULT_OK) {
String m = data.getStringExtra(SecondActivity.EXTRA_MESSAGE);
mTextView.setText(m);
}
}
}
initialize th string m with appropriate content.
String m= " ";
Related
I am trying to get result for dialer Intent using startActivityForResult()
Below is my code for Dialer Intent.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivityForResult(intent, 1234);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1234){
if (resultCode == Activity.RESULT_OK){
Toast.makeText(getApplicationContext(), "result ok", Toast.LENGTH_LONG).show();
}else if (resultCode == Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Result Cancelled", Toast.LENGTH_LONG).show();
}
}
}
whenever I am returning to my activity, Result Cancelled Toast is triggering.
Thanks in advance.
From doc:
ACTION_DIAL
public static final String ACTION_DIAL
You only have the ACTION. If you want to call a number from your application then you just have to put these lines of code into the onClick() method and can get what you want:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent); // no need to use startActivityResult(intent,1234)
Here, If an ACTION_DIAL input is nothing, an empty dialer is started; else getData() is URI of a phone number to be dialed or a tel: <yourURI> of an explicit phone number.
Additionally, there is no "Output" of RESULT_OK or RESULT_CANCELED, Cause, the startActivityResult() doesn't mean anything to ACTION_DIAL but startActivity(intent). Hope it helps.
why am I getting RESULT_CANCELED instead of RESULT_OK.
ACTION_DIAL does not return a result. If you read the documentation for ACTION_DIAL, you will see "Output: nothing". Hence, you will usually get RESULT_CANCELED. Only activities designed for use with startActivityForResult() will return a result code.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of CropImageActivity
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
Toast.makeText(
this, "Cropping successful, Sample: " + result.getSampleSize(), Toast.LENGTH_LONG)
.show();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
When I take a picture my app crashes. The exception on my phone is:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=203, result=-1, data=Intent { act=inline-
data (has extras) }} to activity
{com.example.michael.matcalc/com.example.michael.matcalc.Crop}:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri
com.theartofdev.edmodo.cropper.CropImage$ActivityResult.getUri()' on a
null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4355)
...
I think that something goes wrong with the data. It crashes at this line:
((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
The library I use is this: https://github.com/ArthurHub/Android-Image-Cropper
I would appreciate if somebody could help me.
This is because you're incorrectly using the library. You need to use the following code:
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
instead of:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
It is because when you're calling Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); you're not specifically tell the Crop library to handle the image.
Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.
I am android beginner, my aim is select image and display on my ImageView, I use below code but in fragment onActivity result return null pointer exception.
This is my fragmentA.java
#Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == getActivity().RESULT_OK) {
try {
final Uri imageUri = imageReturnedIntent.getData();
Log.v("imageUri", imageUri.toString());
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
initImage();
break;
}
}
I used below code for call gallery intent
image.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if( Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT )
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent,"Select Picture"), SELECT_PHOTO);
}
else
{
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
}
});
This is my error report ..
java.lang.RuntimeException: Unable to resume activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.caneda.citycenter.myaccount.FragmentTabProfile.onActivityResult(FragmentTabProfile.java:217)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at com.caneda.citycenter.myaccount.AccountMainActivity.onActivityResult(AccountMainActivity.java:102)
at android.app.Activity.dispatchActivityResult(Activity.java:5305)
I think the line
resultCode == getActivity().RESULT_OK
is causing the problem
USE
resultCode == Activity.RESULT_OK
because RESULT_OK is static
However you can debug to test which code is causing NullPointerException
Actully answer is really silly, I have initialize
conditional based
if(saveInstaceState == null) { ivUpload = (ImageView) }
It should be outside of condition.
That's why it cause nullPointerException by ivUpload.
Thanks you so much for you support.
There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?