触发onclick方法时收到错误

问题描述 投票:-1回答:1

所以我已经在我的xml布局中为我的保存按钮定义了onclick方法,然后在我的对话框片段中创建了该方法,但是当点击保存按钮时,应用程序崩溃并且我收到此错误: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'不确定为什么看到就像我在xml和dialogfragment类中定义方法一样

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类:

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());
    }
}

}

android onclick android-dialogfragment buttonclick
1个回答
1
投票

正如错误所示,Could not find method saveClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class

您需要在主机Activity中定义方法saveClicked。片段不是为此而设计的。请看看:Android Fragment onClick button Method

© www.soinside.com 2019 - 2024. All rights reserved.