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

后端 未结 5 841
甜味超标
甜味超标 2021-02-18 13:59

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 \"A

相关标签:
5条回答
  • 2021-02-18 14:00

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

    0 讨论(0)
  • 2021-02-18 14:01

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

    0 讨论(0)
  • 2021-02-18 14:01

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

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

    0 讨论(0)
  • 2021-02-18 14:05

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

    For example

    You have TextView with id result

     <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

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

    It gone crash you app.

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

    0 讨论(0)
  • 2021-02-18 14:16

    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:...}
    
    0 讨论(0)
提交回复
热议问题