6

I have tried a lot to find solution. As Same Question found but It doesn't have answer what I want.

I have following XML File:

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

I am getting Errors like:

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)

I don't know why it happens. Is there any solution? Thank you.

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

4 Answers4

8

This is related to the way AppCompat inflates your layout. Try adding a programmatic onClick listener to your buttons rather than doing it within XML.

There's a discussion about it on Google Code. It's because you are using the AppCompat Buttons.

Peter Marozzi
  • 91
  • 1
  • 5
  • Thank you Peter! It is weird that it does not work with xml... and the error message did not make any sense to me at all. May be it is just me... LOL! – Vincy Apr 19 '17 at 12:46
1

You could use butterknife annotations instead

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

In my case the problem was from setting the android:theme attribute to the view, (and also is related to the AppCompat library ... see @Peter Marozzi's answer).

So I simply removed the android: namespace from this line (from the view's style):

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

and make it likes:

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

and it works fine.

The interactive note is the problem is only on high-level APIs (23 I tested) and on low-level APIs (16 and 19 I tested) both methods (with or without android: namespace) work.

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
0

It worked for me to register the listener on the current DialogFragment class:

@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
}