膨胀自定义视图时出错

Error inflating a custom view

我有一个名为 MyCustomView 的 class,它扩展了 CardView 并实现了 GestureDetectorCompat.OnGestureListener

当我充气 MyCustomView 时,我不断收到以下错误:

Unable to start activity  android.view.InflateException: Binary XML file line #26: Error inflating class com.ridever.scrollableviews.MyCardView
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

注意:

  1. 我已经在布局文件中声明了包名。
  2. 我已经声明了我的域名,即 xmlns:app.....
  3. 我已经定义了它的 3 个超级 class 构造函数。

在阅读了大多数关于同一问题的 post 之后,我不知道哪里出了问题。有什么想法吗?

这是我的代码和布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.CardView
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/cardview"
    android:layout_alignParentTop="true"
    app:cardElevation="1dp">

    <!--ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/background"/-->

</android.support.v7.widget.CardView>


<com.ridever.scrollableviews.MyCardView
    android:id="@+id/cardview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="-100dp"
    app:cardElevation="1dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/scrollable" />

</com.ridever.scrollableviews.MyCardView>

MyCardView 定义在这里:

package com.ridever.scrollableviews;

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MyCardView extends CardView implements                 GestureDetector.OnGestureListener{
private static final String CTAG = "MyCardView";

private GestureDetectorCompat gestureDetectorCompat;


public MyCardView(Context context) {
    super(context);
    gestureDetectorCompat = new GestureDetectorCompat(getContext(), this);
}

public MyCardView(Context context, AttributeSet attrs, GestureDetectorCompat gestureDetectorCompat) {
    super(context, attrs);
    gestureDetectorCompat = new GestureDetectorCompat(getContext(), this);

}

public MyCardView(Context context, AttributeSet attrs, int defStyleAttr, GestureDetectorCompat gestureDetectorCompat) {
    super(context, attrs, defStyleAttr);
    gestureDetectorCompat = new GestureDetectorCompat(getContext(), this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    gestureDetectorCompat.onTouchEvent(event);
    return super.onTouchEvent(event);
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    final String MTAG = ".onFling()";
    Log.i(String.format("%s%s", CTAG, MTAG), "...");

    return true;
}

您是否尝试从这两个构造函数中删除额外的参数?

public MyCardView(Context context, AttributeSet attrs, GestureDetectorCompat gestureDetectorCompat) {

public MyCardView(Context context, AttributeSet attrs, int defStyleAttr, GestureDetectorCompat gestureDetectorCompat) {

额外参数我的意思是 GestureDetectorCompat 因为你修改了 CardView 的合同(合同我的意思是可用的构造函数)我猜 Android 正在尝试实例化你的 MyCardView使用其中之一。