当 Onclick 在 fragment xml 中操作时,应用程序崩溃

When Onclick operates in fragment xml, app crashes

我是开发领域的新手,我只是在尝试片段。我在我的片段 XML 文件之一中定义了一个 onClick 事件,但是当我单击该按钮时,我的应用程序崩溃了。

我推测发生这种情况是因为我的 onClick 事件是在我的片段而不是我的 MainActivity 中定义的,所以我只是想知道让我的按钮工作的最佳做​​法是什么。我应该只将按钮传递到我的片段,还是应该将我的 onClick 事件移动到我的 MainActivity 然后从我的片段中引用它?

***************** 这是代码 ****************

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="1.5">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginRight="5dp"
            android:gravity="right"
            android:layout_weight="2">

            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="150dp"
                android:id="@+id/txt_textInputLayout_three"
                android:layout_height="wrap_content"
                app:startIconDrawable="@drawable/ic_baseline_calendar_today_24">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/txt_from_user1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="From"
                    android:onClick="openDataPicker_from"
                    android:inputType="text" />

            </com.google.android.material.textfield.TextInputLayout>

        </LinearLayout>
 <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginLeft="5dp"
            android:gravity="left"
            android:layout_weight="2">


            <com.google.android.material.textfield.TextInputLayout
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="150dp"
                android:id="@+id/txt_textInputLayout_four"
                android:layout_height="wrap_content"
                app:startIconDrawable="@drawable/ic_baseline_calendar_today_24">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/txt_to_user1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="To"
                    android:onClick="openDatePicker_to"
                    android:inputType="text" />

            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>

    </LinearLayout>

Java 文件***

 public void openDatePicker_from(View view) {
        dataPickerDialog_from.show();

    }

    public void openDatePicker_to(View view) {
        dataPickerDialog_to.show();


    }

这是错误

java.lang.IllegalStateException: Could not find method openDataPicker(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.textfield.TextInputEditText with id 'txt_from_user1'
        at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5368)

java.lang.IllegalStateException: Could not find method openDataPicker(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.textfield.TextInputEditText with id 'txt_from_user1'
        at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5368)

这个错误是因为它无法在 activity class 中找到方法 openDataPicker(View)(即使你已经将它添加到片段 class)。

在片段中使用 android:onClick 属性与在活动中不完全相同;如果您在 activity 或片段中使用它,它希望仅在 activity class.

中看到方法 openDataPicker()

Check here 查看如何在片段中使用 android:onClick 属性,或者您通常可以通过编程方式使用 myButton.setOnClickListener()

更新:

So can you tell me according to the code, what I want to change in the code clearly

您可以在片段中使用setOnClickListener()

TextInputEditText editTextFrom = requireView().findViewById(R.id.txt_from_user1);
editTextFrom.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dataPickerDialog_from.show();
    }
});


TextInputEditText editTextTo = requireView().findViewById(R.id.txt_to_user1);
editTextTo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dataPickerDialog_to.show();
    }
});

然后从 XML 中删除 onClick 属性:

android:onClick="openDatePicker_to"


android:onClick="openDataPicker_from"

成功了,试试这个

FromDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataPickerDialog_from.show();
            }
        });

        ToDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataPickerDialog_to.show();
            }
        });