{"record":{"id":"3796e4b893cde0fb","repo":"JakeWharton/butterknife","slug":"view-with-id-for-was-of-the-wrong-type","errorCode":null,"errorMessage":"View '{}' with ID {} for {} was of the wrong type. See cause for more info.","messagePattern":"View '(.+?)' with ID (.+?) for (.+?) was of the wrong type\\. See cause for more info\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"butterknife-runtime/src/main/java/butterknife/internal/Utils.java","lineNumber":105,"sourceCode":"        + id\n        + \" for \"\n        + who\n        + \" was not found. If this view is optional add '@Nullable' (fields) or '@Optional'\"\n        + \" (methods) annotation.\");\n  }\n\n  public static <T> T findRequiredViewAsType(View source, @IdRes int id, String who,\n      Class<T> cls) {\n    View view = findRequiredView(source, id, who);\n    return castView(view, id, who, cls);\n  }\n\n  public static <T> T castView(View view, @IdRes int id, String who, Class<T> cls) {\n    try {\n      return cls.cast(view);\n    } catch (ClassCastException e) {\n      String name = getResourceEntryName(view, id);\n      throw new IllegalStateException(\"View '\"\n          + name\n          + \"' with ID \"\n          + id\n          + \" for \"\n          + who\n          + \" was of the wrong type. See cause for more info.\", e);\n    }\n  }\n\n  public static <T> T castParam(Object value, String from, int fromPos, String to, int toPos,\n      Class<T> cls) {\n    try {\n      return cls.cast(value);\n    } catch (ClassCastException e) {\n      throw new IllegalStateException(\"Parameter #\"\n          + (fromPos + 1)\n          + \" of method '\"\n          + from","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/JakeWharton/butterknife/blob/fcdebedf3276096db2f51bf6372b849b5a9c75ed/butterknife-runtime/src/main/java/butterknife/internal/Utils.java#L87-L123","documentation":"Thrown by Utils.castView when the view found for a bound ID exists but cannot be cast to the annotated field's type. The original ClassCastException is preserved as the cause; the message identifies the resource entry name, ID, and the binding target ('who') so you can compare XML vs Java types.","triggerScenarios":"`@BindView(R.id.title) TextView title;` where XML declares `<androidx.appcompat.widget.AppCompatEditText android:id=\"@id/title\" ...>` — any mismatch between the XML widget class and the annotated field type, e.g. EditText vs TextView is fine (upcast) but TextView vs ImageView, or a custom view replaced by its subclass, throws.","commonSituations":"Renaming/replacing a widget in XML during a redesign while the Java field keeps the old type; merging layouts where an ID collides across include/merge tags; support-library migrations changing actual inflated classes; a wrong <include> bringing another layout's view with the same ID first in the hierarchy.","solutions":["Align the types: change the field type to the XML widget's class (or a supertype of it).","Check for ID collisions across includes/merge and rename the duplicate ID.","If polymorphism is expected (same ID different types across variants), bind to the common supertype (e.g. View or TextView)."],"exampleFix":"<!-- before -->\n<ImageView android:id=\"@+id/media\" ... />\n@BindView(R.id.media) TextView media; // wrong type\n\n<!-- after -->\n<ImageView android:id=\"@+id/media\" ... />\n@BindView(R.id.media) ImageView media;","handlingStrategy":"type-guard","validationCode":"View v = sourceView.findViewById(R.id.media);\nif (v != null && !TextView.class.isAssignableFrom(v.getClass())) {\n  // XML type differs from field type — fix layout or field before bind\n}","typeGuard":"static boolean viewMatchesFieldType(View v, Class<?> fieldType) {\n  return v == null || fieldType.isAssignableFrom(v.getClass());\n}","tryCatchPattern":null,"preventionTips":["Keep XML widget types and Java field types aligned; bind to a common supertype where variants differ.","Watch for duplicate IDs across include/merge — rename collisions.","When swapping widgets in XML, grep Java/Kotlin for @BindView references to that ID."],"tags":["butterknife","classcast","view-lookup","layout","field-type"],"backgroundTag":null,"analyzedSha":"fcdebedf3276096db2f51bf6372b849b5a9c75ed","analyzedAt":"2026-08-14T10:13:55.083Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}