【问题标题】:Custom view is missing constructor used by tools for adapter自定义视图缺少适配器工具使用的构造函数
【发布时间】:2013-06-12 10:22:37
【问题描述】:

我收到以下警告:

自定义视图 com/example/view/adapter/SomeAdapter 丢失 工具使用的构造函数: (Context) 或 (Context,AttributeSet) 或 (上下文,属性集,int)

在我的类 SomeAdapter 中,它扩展了一些 BaseAdapter,它扩展了 ArrayAdapter

public class SomeAdapter extends BaseAdapter{}
public abstract class BaseAdapter extends ArrayAdapter<SomeModel>{}

警告存在于具体适配器中,但不存在于抽象 BaseAdapter 中。 在这种情况下,有人听说过这个警告吗?

AFAIK Android 通过ViewConstructorDetector 检查超类的名称来检查类是否扩展视图:

 private static boolean isViewClass(ClassContext context, ClassNode node) {
    String superName = node.superName;
    while (superName != null) {
        if (superName.equals("android/view/View")                //$NON-NLS-1$
                || superName.equals("android/view/ViewGroup")    //$NON-NLS-1$
                || superName.startsWith("android/widget/")       //$NON-NLS-1$
                && !((superName.endsWith("Adapter")              //$NON-NLS-1$
                        || superName.endsWith("Controller")      //$NON-NLS-1$
                        || superName.endsWith("Service")         //$NON-NLS-1$
                        || superName.endsWith("Provider")        //$NON-NLS-1$
                        || superName.endsWith("Filter")))) {     //$NON-NLS-1$
            return true;
        }

        superName = context.getDriver().getSuperClass(superName);
    }

    return false;
}

据我所知,我的类名与上面的模式不匹配。 有谁知道如何修复或禁止此警告?

BaseAdapter 中的getView():

@Override
public final View getView(final int position, final View convertView, final ViewGroup parent) {
    View view = convertView;
    if (null == view) {
        view = createNewView(parent, position);
    } else {
        reuseOldView(view, position);
    }
    return view;
}

【问题讨论】:

  • 什么实现了你的适配器?你有自定义视图吗?
  • 不,我没有实现任何东西。只是扩展 ArrayAdapter。
  • 出于什么目的?适配器不能单独工作。你想用它做什么?
  • 我在(标准)ListView 中使用了可能的适配器。

标签: android warnings adapter android-arrayadapter listadapter


【解决方案1】:

在您的 CustomView 类中添加构造函数:

public CustomView(Context context) {
    super(context);
}
public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

【讨论】:

  • 我没有任何 CusonView。我只有我的适配器。而 ArrayAdapter 不提供这样的构造函数。
  • 你能从你的适配器发布你的 getView() 方法吗?谢谢
  • 我不知道这有什么好处,但给你。 getView() 在 BaseAdapter 中实现,但在警告所在的 SomeAdapter 中没有实现。
【解决方案2】:

请在 Kotlin 中尝试以下操作:-

 class CustomView: View {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
    }
}

【讨论】:

    【解决方案3】:

    一些布局工具(例如 Eclipse 的 Android 布局编辑器)需要找到具有以下签名之一的构造函数: * View(Context context) * View(Context context, AttributeSet attrs) * View(Context context, AttributeSet attrs, int defStyle)

    如果您不使用这些东西,而只是想为您的所有项目消除此警告,请转到:

    窗口 -> 首选项 -> Android -> Lint 错误检查。

    从列表中找到 ViewConstructor 并将严重性设置为 'ignore'

    【讨论】:

    • 这会影响布局编辑器吗?我不明白的是为什么 Lint 认为我的适配器是一个视图。
    【解决方案4】:

    您正在使用自定义 View 类,您需要将缺少的构造函数添加到您的类中。

    在 Java 中,您可以使用 Jarvis 的回答:

    class YourCustomView {
        public YourCustomView(Context context) {
            super(context);
        }
        public YourCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    }
    

    在 Kotlin 中更紧凑:

    class YourCustomView @JvmOverloads constructor(
            context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : View(context, attrs, defStyleAttr)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2011-02-22
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      相关资源
      最近更新 更多