Francesco10
Lurker
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 androidnClick 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
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
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
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 androidnClick 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
Java:
public class EmailFragment extends Fragment {
EmailCreated activity;
public interface EmailCreated {
String onEmailCreated(Bundle emailBundle);
}
public EditText subjectLineFrag, emailFrag;
public String emails, subject;
public Button confirmFrag;
public Bundle emailBundle;
@Nullable
@org.jetbrains.annotations.Nullable
@Override
public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.email_fragment, container, false);
emailFrag = view.findViewById(R.id.emailFrag);
subjectLineFrag = view.findViewById(R.id.subjectLineFrag);
confirmFrag = view.findViewById(R.id.confirmFrag);
return view;
}
public Bundle storeEmail(View v, EditText subjectLineFrag, EditText emailFrag) {
String emails = emailFrag.getText().toString();
String subject = subjectLineFrag.getText().toString();
emailBundle.putString("message", emails);
emailBundle.putString("subject", subject);
return emailBundle;
}
@Override
public void onAttach(@NonNull @NotNull Context context) {
//context will refer to the attached activity
super.onAttach(context);
activity = (EmailCreated) context;
}
@Override
public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//Implement Fragment Lifecycle
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onAttach(@NonNull @NotNull Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
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:
public class GuestActivity2 extends AppCompatActivity implements EmailFragment.EmailCreated {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guest2);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, new EmailFragment());
transaction.commit();
}
@Override
public String onEmailCreated(Bundle emailBundle) {
return null;
}
}
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