1

I have a fragment in a User Activity that opens up a Map Activity to receive some string variables.The fragment Has Already Recieved 2 String Variables from the User Activity using Bundles.

This is my Method for doing such but i keep getting that error :

And this is the error i get that kick me out

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.project_seraphim_disease_tracker, PID: 17049
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, 
request=66738, result=-1, data=Intent { (has extras) }} to activity 
{com.example.project_seraphim_disease_tracker/com.example.project_seraphim_diseas
e_tracker.User}: java.lang.NullPointerException: Attempt to invoke virtual method 
'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference

These are in both Map Activity and the Fragment that's in User Activity

private static final String SEARCHED_ADDRESS = "searched_address";
private static final String SEARCHED_NAME = "searched_address_name";
private LatLng SearchedLocationlatLng;
private String SearchedlocationAddress;
private String SearchedlocationName;

This is the button that called to a Map activity from a fragment which is in User Activity

btnAddress.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(),Map.class);
        startActivityForResult(intent,IDENTIFIER);
    }
});

This is what i used to Receive the Strings

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (IDENTIFIER) : {
            if (resultCode == Map.RESULT_OK) {
                SearchedlocationAddress=data.getStringExtra(SEARCHED_ADDRESS);
                SearchedlocationName=data.getStringExtra(SEARCHED_NAME);
                editTextSearchLocationBar.setText(SearchedlocationName);
            }
            break;
        }
    }
}

This is in the Map Activity for passing the strings

btnConfirmAddress.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent resultIntent = new Intent();
        resultIntent.putExtra(SEARCHED_NAME,SearchedlocationName);
        resultIntent.putExtra(SEARCHED_ADDRESS,SearchedlocationAddress);
        setResult(Map.RESULT_OK, resultIntent);
        finish();

    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Xin Nian
  • 315
  • 1
  • 2
  • 10
  • if you look at the logs you posted you can see a null pointer exception: it looks like the issue is that the edit text is not set (it's null) when your onActivityResult() runs. are you sure you're setting `editTextSearchLocationBar` to the view it should be? – takecare May 29 '20 at 07:57
  • Yeah that was the part of the problem. Fixed that after searching for a while. I also find my solution to this problem I'm having. All i had to do is call onActivityResult in the User Activity and pass the data to fragment from there. – Xin Nian May 29 '20 at 08:33

1 Answers1

1

The Solution was found on this old post but it still works.

https://stackoverflow.com/a/16449850/13594169

basically When i call startActivityForResult in a fragment the parent activity (Inmy case the User Activity get to handle the data first) Then from there i have to pass the data to the fragment.

Imagein Activities as the parent A and B and Fragment in A as the child A.1. Child A.1 request data from parent B. Parent B give the data to parent A, after parent A receive the data, then it will pass it to child A.1.

So after Calling from fragment A.1

startActivityForResult(intent,IDENTIFIER);

put this in Activity B for passing the required data

Intent resultIntent = new Intent();
resultIntent.putExtra(key_word,String);
setResult(Map.RESULT_OK, resultIntent);

put this in activity A To handel the data received from activity B

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(childTag);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, intent);
    }
}
//If you have your child Fragment as a private member, 
//then use Fragment fragment = myChildFragment;
//to replace the above findFragmentByTag line of code. The rest can be kept unchanged

Then put this in fragment A.1 to receive the data orgin from Activity B

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case (IDENTIFIER) : {
            if (resultCode == Map.RESULT_OK) {
                //Assign data
                //Update Ui
            }
            break;
        }
    }
}
Xin Nian
  • 315
  • 1
  • 2
  • 10