Java 在自定义 BaseAdapter 子类中使用 Butter Knife 导致“无法注入视图”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23448925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:02:23  来源:igfitidea点击:

Using Butter Knife in Custom BaseAdapter SubClass Results in "Unable to inject views" error

javaandroidannotationsbaseadapterbutterknife

提问by reggie3

I'm attempting to use Butter Knife to simplify creation of a custom BaseAdapter class. I'm following the example here: http://jakewharton.github.io/butterknife/under the "Another use is simplifying the view holder pattern inside of a list adapter." section. Unfortunately, I am getting an "Unable to inject views" error each time the ViewHolder is created for each item in the list.

我正在尝试使用 Butter Knife 来简化自定义 BaseAdapter 类的创建。我正在遵循这里的示例:http: //jakewharton.github.io/butterknife/在“另一个用途是简化列表适配器内的视图持有者模式”下。部分。不幸的是,每次为列表中的每个项目创建 ViewHolder 时,我都会收到“无法注入视图”错误。

Here is my code:

这是我的代码:

public class ButterknifeCustomBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
    return arrayListNames.size();
}

@Override
public Name getItem(int iPosition) {
    return arrayListNames.get(iPosition);
}

@Override
public long getItemId(int iID) {
    return 0;
}

LayoutInflater inflater;
ArrayList<Name> arrayListNames = new ArrayList<Name>();
static Context context;
Activity activity;

public ButterknifeCustomBaseAdapter(Context context, ArrayList<Name> names, Activity activity) {
    arrayListNames = names;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}



static class ViewHolder implements View.OnClickListener {
    @InjectView(R.id.textViewFullName) TextView textViewFullName;
    @InjectView(R.id.imageButtonDeleteName) TextView imageButtonDeleteName;
    @OnClick(R.id.imageButtonDeleteName)
    public void onClick(View view) {
        ((NameActivity)context).DeleteUser((Name)view.getTag());
    }

    public ViewHolder(View view) {
        ButterKnife.inject(this, view);
    }
}

@Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;

    if (convertView != null) {
        viewHolder = (ViewHolder) convertView.getTag();
    } else {
        convertView = inflater.inflate(R.layout.item_name, parent, false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    }

    viewHolder.textViewFullName.setText(((Name)getItem(position)).GetFullName());
    L.l("ArrayAdapterName", ((Name)getItem(position)).GetID() + " inserted in list | position = " + position);
    viewHolder.imageButtonDeleteName.setTag((Name)getItem(position));
    return convertView;
}

}

}

The error happens at the "ButterKnife.Inject(this, view);" line. It also happens each time the view holder is created for each item I am putting in the list. Does anybody know how to make this work the way it should?

错误发生在“ButterKnife.Inject(this, view);” 线。每次为我放入列表的每个项目创建视图持有者时,也会发生这种情况。有谁知道如何使这项工作按应有的方式进行?

In reply to Jake Wharton's comment First, let me say thank you for developing this tool. It has made android development much more enjoyable than it was when I first started.

回复 Jake Wharton 的评论 首先,感谢您开发此工具。与我刚开始时相比,它使 android 开发变得更加有趣。

Here is the complete stacktrace:

这是完整的堆栈跟踪:

05-04 07:29:01.991    2424-2424/com.murach.databasehomework E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to inject views for com.murach.databasehomework.ButterknifeCustomBaseAdapter$ViewHolder@52700878
            at butterknife.ButterKnife.inject(ButterKnife.java:221)
            at butterknife.ButterKnife.inject(ButterKnife.java:184)
            at com.murach.databasehomework.ButterknifeCustomBaseAdapter$ViewHolder.<init>(ButterknifeCustomBaseAdapter.java:59)
            at com.murach.databasehomework.ButterknifeCustomBaseAdapter.getView(ButterknifeCustomBaseAdapter.java:70)
            at android.widget.AbsListView.obtainView(AbsListView.java:2177)
            at android.widget.ListView.makeAndAddView(ListView.java:1840)
            at android.widget.ListView.fillDown(ListView.java:675)
            at android.widget.ListView.fillFromTop(ListView.java:736)
            at android.widget.ListView.layoutChildren(ListView.java:1655)
            at android.widget.AbsListView.onLayout(AbsListView.java:2012)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at butterknife.ButterKnife.inject(ButterKnife.java:216)
????????????at butterknife.ButterKnife.inject(ButterKnife.java:184)
????????????at com.murach.databasehomework.ButterknifeCustomBaseAdapter$ViewHolder.<init>(ButterknifeCustomBaseAdapter.java:59)
????????????at com.murach.databasehomework.ButterknifeCustomBaseAdapter.getView(ButterknifeCustomBaseAdapter.java:70)
????????????at android.widget.AbsListView.obtainView(AbsListView.java:2177)
????????????at android.widget.ListView.makeAndAddView(ListView.java:1840)
????????????at android.widget.ListView.fillDown(ListView.java:675)
????????????at android.widget.ListView.fillFromTop(ListView.java:736)
????????????at android.widget.ListView.layoutChildren(ListView.java:1655)
????????????at android.widget.AbsListView.onLayout(AbsListView.java:2012)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
????????????at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
????????????at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
????????????at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
????????????at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
????????????at android.view.Choreographer.doCallbacks(Choreographer.java:562)
????????????at android.view.Choreographer.doFrame(Choreographer.java:532)
????????????at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
????????????at android.os.Handler.handleCallback(Handler.java:730)
????????????at android.os.Handler.dispatchMessage(Handler.java:92)
????????????at android.os.Looper.loop(Looper.java:137)
????????????at android.app.ActivityThread.main(ActivityThread.java:5103)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:525)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
????????????at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView
            at com.murach.databasehomework.ButterknifeCustomBaseAdapter$ViewHolder$$ViewInjector.inject(ButterknifeCustomBaseAdapter$ViewHolder$$ViewInjector.java:13)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:525)
????????????at butterknife.ButterKnife.inject(ButterKnife.java:216)
????????????at butterknife.ButterKnife.inject(ButterKnife.java:184)
????????????at com.murach.databasehomework.ButterknifeCustomBaseAdapter$ViewHolder.<init>(ButterknifeCustomBaseAdapter.java:59)
????????????at com.murach.databasehomework.ButterknifeCustomBaseAdapter.getView(ButterknifeCustomBaseAdapter.java:70)
????????????at android.widget.AbsListView.obtainView(AbsListView.java:2177)
????????????at android.widget.ListView.makeAndAddView(ListView.java:1840)
????????????at android.widget.ListView.fillDown(ListView.java:675)
????????????at android.widget.ListView.fillFromTop(ListView.java:736)
????????????at android.widget.ListView.layoutChildren(ListView.java:1655)
????????????at android.widget.AbsListView.onLayout(AbsListView.java:2012)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
????????????at android.view.View.layout(View.java:14289)
????????????at android.view.ViewGroup.layout(ViewGroup.java:4562)
????????????at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
????????????at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
????????????at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
????????????at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
????????????at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
????????????at android.view.Choreographer.doCallbacks(Choreographer.java:562)
????????????at android.view.Choreographer.doFrame(Choreographer.java:532)
????????????at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
????????????at android.os.Handler.handleCallback(Handler.java:730)
????????????at android.os.Handler.dispatchMessage(Handler.java:92)
????????????at android.os.Looper.loop(Looper.java:137)
????????????at android.app.ActivityThread.main(ActivityThread.java:5103)
????????????at java.lang.reflect.Method.invokeNative(Native Method)
????????????at java.lang.reflect.Method.invoke(Method.java:525)
????????????at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
????????????at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
????????????at dalvik.system.NativeStart.main(Native Method)

采纳答案by ViliusK

Check if your @InjectViews has correct type. I've used ImageViewinstead of LinearLayout. That might be your problem too.

检查您的@InjectViews 是否具有正确的类型。我用过ImageView而不是LinearLayout. 那也可能是你的问题。

Update:

更新:

Make sure you are not using ButterKnife's @OnItemClick(R.id.non_list_view)with a non ListView. I was using it for a android.support.v7.widget.RecyclerViewwhich was causing following exception:

确保您没有将 ButterKnife@OnItemClick(R.id.non_list_view)与非ListView. 我将它用于android.support.v7.widget.RecyclerView导致以下异常的原因:

java.lang.RuntimeException: Unable to inject views for MyFragment{... id=.... android:switcher:...}

回答by Gon?alo Araújo

I had a similar problem with ButterKnife, but the reason was that I was inflating my fragment with the wrong layout.

ButterKnife 也有类似的问题,但原因是我用错误的布局来膨胀我的片段。

(I know the question was already answered, but I decided to post my solution in case someone had the same issue)

我知道问题已经回答了,但我决定发布我的解决方案,以防有人遇到同样的问题

回答by ViliusK

Also check if you are inflating correct R.layout.file. If not - necessary views are not found and this error occurs.

还要检查您是否正在膨胀正确的R.layout.文件。如果不是 - 找不到必要的视图,则会发生此错误。

回答by Hitesh Sahu

Those who are still looking it happens if you are trying to bind wrong View type from XML in Java file it .

如果您试图从 Java 文件中的 XML 绑定错误的 View 类型,那些仍在寻找它的人就会发生。

For example

例如

You have TextView with id result

你有带有 id 结果的 TextView

 <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_margin="3dp"
        android:layout_weight="2"
        android:background="#fff"
        android:text="Calculator"
        android:textSize="25sp" />

And you try to bind it with Button in java file

并且您尝试将其与 java 文件中的 Button 绑定

@BindView(R.id.result) Button result;

@BindView(R.id.result) 按钮结果;

It gone crash you app.

它让你的应用程序崩溃了。

Butterknife will throw exception for illegal casting of views as Unable to Inject View Error

Butterknife 将因无法注入视图错误而抛出非法视图的异常

回答by Langusten Gustel

For me it turned out that the ViewBinderwas not correctly refreshed. A clean project-build solved this issue.

对我来说,结果ViewBinder是没有正确刷新。一个干净的项目构建解决了这个问题。