1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help with sending info from Fragments

Discussion in 'Android Development' started by Francesco10, Jul 26, 2021.

  1. Francesco10

    Francesco10 Lurker
    Thread Starter

    Hi guys, new to app development and new to the forum. Am actually enjoying developing apps as opposed to programs because of the layout designer in android studio but recently have run into a real annoying issue that I can't seem to solve. I'm trying to write an app that allows a user to send one email that personalizes individiually to each sitting politician in my country. Had learned that if something was going to be used in more than one place a fragment was better than an activity. But, I cannot for the life of me figure out how to send the info I want from the fragment to the parent activity.

    I have been changing this around a few times but thought I had finally got it until I ran into an illegal state exception (full exception is : java.lang.IllegalStateException: Could not find method storeEmail(View) in a parent or ancestor Context for android:eek:nClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'confirmFrag') when hitting the button to confirm the input. Code of the fragment and activity it's attached to below

    Code (Java):
    1. public class EmailFragment extends Fragment {
    2.     EmailCreated activity;
    3.  
    4.     public interface EmailCreated {
    5.         String onEmailCreated(Bundle emailBundle);
    6.     }
    7.  
    8.     public EditText subjectLineFrag, emailFrag;
    9.     public String emails, subject;
    10.     public Button confirmFrag;
    11.     public Bundle emailBundle;
    12.  
    13.     @Nullable
    14.     @org.jetbrains.annotations.Nullable
    15.     @Override
    16.     public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    17.         View view = inflater.inflate(R.layout.email_fragment, container, false);
    18.         emailFrag = view.findViewById(R.id.emailFrag);
    19.         subjectLineFrag = view.findViewById(R.id.subjectLineFrag);
    20.         confirmFrag = view.findViewById(R.id.confirmFrag);
    21.  
    22.         return view;
    23.     }
    24.  
    25.     public Bundle storeEmail(View v, EditText subjectLineFrag, EditText emailFrag) {
    26.         String emails = emailFrag.getText().toString();
    27.         String subject = subjectLineFrag.getText().toString();
    28.         emailBundle.putString("message", emails);
    29.         emailBundle.putString("subject", subject);
    30.         return emailBundle;
    31.     }
    32.    
    33.     @Override
    34.     public void onAttach(@NonNull @NotNull Context context) {
    35.         //context will refer to the attached activity
    36.         super.onAttach(context);
    37.         activity = (EmailCreated) context;
    38.     }
    39.  
    40.     @Override
    41.     public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    42.         super.onCreate(savedInstanceState);
    43.     }
    44.  
    45.  
    46.     //Implement Fragment Lifecycle
    47.     @Override
    48.     public void onStart() {
    49.         super.onStart();
    50.     }
    51.  
    52.     @Override
    53.     public void onResume() {
    54.         super.onResume();
    55.     }
    56.  
    57.     @Override
    58.     public void onPause() {
    59.         super.onPause();
    60.     }
    61.  
    62.     @Override
    63.     public void onStop() {
    64.         super.onStop();
    65.     }
    66.  
    67.     @Override
    68.     public void onDestroyView() {
    69.         super.onDestroyView();
    70.     }
    71.  
    72.     @Override
    73.     public void onDestroy() {
    74.         super.onDestroy();
    75.     }
    76.  
    77.     @Override
    78.     public void onAttach(@NonNull @NotNull Activity activity) {
    79.         super.onAttach(activity);
    80.     }
    81.  
    82.     @Override
    83.     public void onDetach() {
    84.         super.onDetach();
    85.     }
    86. }
    I thought that by declaring the variables publicly instead of privately the exception would go away but it has not. Here is the code for the activity that wants the data sent to it, although it's very bare bones, had been working on the fragment

    Code (Text):
    1. public class GuestActivity2 extends AppCompatActivity implements EmailFragment.EmailCreated {
    2.  
    3.     @Override
    4.     protected void onCreate(Bundle savedInstanceState) {
    5.         super.onCreate(savedInstanceState);
    6.         setContentView(R.layout.activity_guest2);
    7.  
    8.         FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    9.         transaction.replace(R.id.container, new EmailFragment());
    10.         transaction.commit();
    11.     }
    12.  
    13.     @Override
    14.     public String onEmailCreated(Bundle emailBundle) {
    15.  
    16.         return null;
    17.     }
    18. }
    I have no idea why this is not working. All I want it to do is send the stuff to the bundle so I can continue with the app.

    I don't know if my issue is a small one or a large structural one caused by making a huge mistake in the process of sending it. Obviously if it's a small mistake in the code I'd be much obliged if someone could show me but would also really appreciate if someone could tell me if I'm making a gigantic structural mistake with my code. I've been confused by most tutorials. Have no issue rewriting my code to fit a better template for sending info from fragments, I am just at a bit of a loss as to how to procede
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...