期望的接收器类型,但得到android.support.v7.widget.TintContextWrapper

问题描述 投票:4回答:4

我已经尝试了很多寻找解决方案。正如Same Question发现但它没有回答我想要的。

我有以下XML文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/add_appointment_parent_layout"
    tools:context="com.potionowl.app.activity.AppointmentAddActivity">

    <include layout="@layout/layout_toolbar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/padding_small"
        android:layout_margin="@dimen/padding_medium"
        android:layout_height="match_parent">

        <android.support.v7.widget.AppCompatSpinner
            android:id="@+id/add_appointment_doctor_spinner"
            android:layout_width="match_parent"
            android:prompt="@string/string_select_doctor_patient"
            android:spinnerMode="dialog"
            android:layout_margin="@dimen/padding_medium"
            android:layout_height="wrap_content"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_margin="@dimen/padding_medium"
            android:layout_height="wrap_content"
            android:baselineAligned="false">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/add_appointment_date_title"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_margin="@dimen/padding_very_small"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/add_appointment_date_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/string_add_app_date_for_appointment"
                    android:focusable="false"
                    android:onClick="PickDate"
                    android:singleLine="true"
                    android:inputType="datetime"/>
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/add_appointment_time_title"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_margin="@dimen/padding_very_small"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/add_appointment_time_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/string_add_app_time_for_appointment"
                    android:focusable="false"
                    android:onClick="PickTime"
                    android:singleLine="true"
                    android:inputType="datetime"/>
            </android.support.design.widget.TextInputLayout>
        </LinearLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/add_appointment_symptoms_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/padding_medium">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/add_appointment_symptoms_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="5"
                android:gravity="start"
                android:hint="@string/string_add_app_symptoms"
                android:lines="5"/>
        </android.support.design.widget.TextInputLayout>

        <android.support.v7.widget.AppCompatButton
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            style="@style/Base.Widget.AppCompat.Button.Colored"
            android:layout_margin="@dimen/padding_medium"
            android:onClick="addAppointmentButtonClick"
            android:layout_height="wrap_content"
            android:text="@string/apply_for_appointment"/>
    </LinearLayout>
</LinearLayout>

我得到的错误如下:

05-10 23:27:16.942 13455-13455/com.potionowl.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.potionowl.app, PID: 13455
java.lang.IllegalArgumentException: Expected receiver of type com.potionowl.app.activity.AppointmentAddActivity, but got android.support.v7.widget.TintContextWrapper
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4456)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我不知道为什么会这样。有什么解决方案吗?谢谢。

android android-activity android-appcompat appcompatactivity
4个回答
4
投票

这与AppCompat扩展布局的方式有关。尝试将一个程序化的onClick侦听器添加到按钮,而不是在XML中执行。

有关它的讨论on Google Code。这是因为您正在使用AppCompat按钮。


1
投票

您可以使用butterknife注释

@OnClick(R.id.IdForAppCompatButton)
public void doAction() {doSomething();}

1
投票

在我的情况下,问题是将android:theme属性设置为视图,(并且还与AppCompat库有关...请参阅@Peter Marozzi's answer)。

所以我只是从这一行(从视图的android:)中删除了style命名空间:

<item name="android:theme">@style/someTheme</item>

并使它喜欢:

<item name="theme">@style/someTheme</item>

它工作正常。

交互式说明的问题仅在于高级API(我测试过的23个)和低级API(我测试过的16和19个)两种方法(有或没有android:名称空间)都有效。


0
投票

我可以在当前的DialogFragment类上注册监听器:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    dialogView = inflater.inflate(/*your layout*/, container, false);
    dialogView.findViewById(R.id.close_button).setOnClickListener(this);

    return dialogView;
}

@Override
public void onClick(View v) {
    //Handle the button click event here
}
© www.soinside.com 2019 - 2024. All rights reserved.