Сала,
Я написал пользовательскую кнопку для поддержки векторных изображений. Это прекрасно работает, но когда я пытаюсь зарегистрировать onClick
слушатель в XML, я получаю следующую ошибку
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Could not find accessor com.tomycab.fragments.NoTripFragment.openPromoDialog and android.view.View has 0 abstract methods, so is not resolved as a listener
file:C:\Users\Ramdane\AndroidStudioProjects\NEW OLD\tomycab\app\src\main\res\layout\frag_no_trip.xml
loc:261:51 - 261:71
****\ data binding error ****
Вот мой CustomButton
код класса
package com.tomycab.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;
import android.view.View;
import com.tomycab.R;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by ramdane on 9/18/17.
*/
public class CustomButton extends AppCompatButton implements View.OnClickListener {
public static final Logger logger = Logger.getLogger(CustomButton.class.getName());
private String onClickName;
private OnClickListener listener;
private final Context context;
private boolean inited = false;
public CustomButton(Context context) {
super(context);
this.context = context;
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
initAttrs(context, attrs);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
initAttrs(context, attrs);
}
void initAttrs(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomTextView);
try {
Drawable drawableLeft = null;
Drawable drawableRight = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawableLeft = attributeArray.getDrawable(R.styleable.CustomTextView_drawableLeftCompat);
drawableRight = attributeArray.getDrawable(R.styleable.CustomTextView_drawableRightCompat);
drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat);
drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat);
} else {
final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableLeftCompat, -1);
final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableRightCompat, -1);
final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, -1);
final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, -1);
if (drawableLeftId != -1)
drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
if (drawableRightId != -1)
drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
if (drawableBottomId != -1)
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
if (drawableTopId != -1)
drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
}
int drawableTint = attributeArray.getColor(R.styleable.CustomTextView_drawableTintCompat,
ContextCompat.getColor(context, R.color.colorBlack));
if (drawableLeft != null) {
DrawableCompat.setTint(drawableLeft, drawableTint);
}
if (drawableRight != null) {
DrawableCompat.setTint(drawableRight, drawableTint);
}
if (drawableTop != null) {
DrawableCompat.setTint(drawableTop, drawableTint);
}
if (drawableBottom != null) {
DrawableCompat.setTint(drawableBottom, drawableTint);
}
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
} finally {
attributeArray.recycle();
}
onClickName = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "onClick");
inited = true;
}
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
if (!inited) {
this.listener = l;
super.setOnClickListener(this);
} else {
super.setOnClickListener(l);
}
}
@Override
public void onClick(View view) {
if (listener != null) {
if (onClickName != null) {
try {
Method m = context.getClass().getMethod(onClickName, View.class);
m.invoke(context, view);
} catch (NoSuchMethodException ex){
logger.log(Level.WARNING, null, ex);
} catch (InvocationTargetException ex){
logger.log(Level.WARNING, null, ex);
} catch (IllegalAccessException ex){
logger.log(Level.WARNING, null, ex);
}
} else {
listener.onClick(view);
}
}
}
}
Пример XML
<com.tomycab.custom.CustomButton
android:id="@+id/src_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:padding="10dp"
app:drawableLeftCompat="@drawable/ic_more_vert_black_24dp"
android:layout_toLeftOf="@+id/src_popup"
android:layout_toStartOf="@+id/src_popup"
android:onClick="@{frag::onClick}"
/>
Обратите внимание, что я правильно установил переменную frag и протестировал ее с помощью обычной кнопки, и она отлично работает