So i've defined the onclick method for my save button in my xml layout and then created the method in my dialog fragment, however when ever the save button is clicked, the app crashes and I receive this error: Could not find method saveClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'savebutton'
not really sure why seeing as how i've defined the method in both my xml and dialogfragment class
xml:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.CalendarView
android:id="@+id/calendar_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/activityBackGroundColor">
<RelativeLayout
android:id="@+id/calendar_relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="335dp"
android:background="@color/activityBackGroundColor"
>
<!--Todo:Make button corners rounded-->
<Button
android:id="@+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginRight="296dp"
android:layout_marginBottom="20dp"
android:background="@color/toolBarColor"
android:onClick="saveClicked"
android:text="SAVE"
android:textAlignment="center" />
</RelativeLayout>
</android.widget.CalendarView>
Dialogfragment class:
public class calendarFragment extends DialogFragment {
String TAG = "calendarFragment";
CalendarView calendarView;
AlertDialog.Builder builder;
//save button
Button saveButton;
String date;
public interface OnInputListener{
void sendInput(String input);
}
public OnInputListener mOnInputListener;
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//Use the builder class for convenient dialog construction
builder = new AlertDialog.Builder(getActivity());
//Get the layout inflater
LayoutInflater inflater = requireActivity().getLayoutInflater();
calendarView = (CalendarView) inflater.inflate(R.layout.calendar_layout,null);
saveButton = calendarView.findViewById(R.id.savebutton);
//inflate and set the layouts for the dialog
//pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.calendar_layout, null));
return builder.create();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//calendarView = (CalendarView) inflater.inflate(R.layout.calendar_layout, container,false);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
//Date
date = i+"/"+i1+"/"+i2;
}
});
return calendarView;
}
public void saveClicked(View view) {
mOnInputListener.sendInput(date);
getDialog().dismiss();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
mOnInputListener = (OnInputListener) getActivity();
}
catch (ClassCastException e)
{
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage());
}
}
}