Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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?

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.

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) 
like image 985
reggie3 Avatar asked May 03 '14 19:05

reggie3


1 Answers

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

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.RecyclerView which was causing following exception:

java.lang.RuntimeException: Unable to inject views for MyFragment{... id=.... android:switcher:...} 
like image 92
ViliusK Avatar answered Sep 28 '22 05:09

ViliusK