【问题标题】:When Onclick operates in fragment xml, app crashesOnclick在fragment xml中操作时,app崩溃
【发布时间】:2021-08-04 05:28:32
【问题描述】:

我是开发领域的新手,我只是在尝试片段。我在我的片段 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 android android-fragments


    【解决方案1】:
    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)
    

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

    在片段中使用android:onClick属性与在活动中不完全相同;天气你在活动或片段中使用它,它希望只在活动类中看到方法openDataPicker()。

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

    更新:

    那么你能根据代码告诉我,我想在代码中清楚地改变什么

    您可以在片段中使用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"
    

    【讨论】:

    • 那么你能根据代码告诉我,我想在代码中清楚地改变什么......请。
    • @EshanAG 请查看答案的更新部分
    【解决方案2】:

    成功了,试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多