{
  "id": "android/could-not-find-method-on-view",
  "signature": "Could not find method myClickHandler(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton",
  "signature_zh": "在父类或祖先上下文中找不到为视图类 androidx.appcompat.widget.AppCompatButton 定义的 android:onClick 属性方法 myClickHandler(View)",
  "regex": "Could not find method.*\\(View\\) in a parent or ancestor Context for android:onClick attribute",
  "domain": "android",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The activity or fragment hosting the layout does not implement the onClick method referenced in the XML layout's android:onClick attribute.",
  "root_cause_type": "generic",
  "root_cause_zh": "承载布局的活动或片段未实现 XML 布局中 android:onClick 属性引用的 onClick 方法。",
  "versions": [
    {
      "version": "Android 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 13",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 14",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Android 15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Change the method signature to include a return type like boolean myClickHandler(View v)",
      "why_fails": "The android:onClick handler must be void and accept a single View parameter; any other signature causes a runtime lookup failure.",
      "fail_rate": 0.99,
      "condition": "",
      "sources": []
    },
    {
      "action": "Add the method to a different class like an inner fragment without updating the layout's context",
      "why_fails": "The layout inflation context is the activity, not the fragment, so the method must be in the activity unless using DataBinding.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a lambda expression in XML like android:onClick=\"@{() -> activity.myClickHandler()}\" without DataBinding enabled",
      "why_fails": "Lambda expressions in XML require DataBinding or ViewBinding; otherwise, the system expects a plain method reference.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "In the activity hosting the layout, add the method: public void myClickHandler(View view) { // handle click }. Ensure it's public and takes exactly one View parameter.",
      "success_rate": 0.95,
      "how": "In the activity hosting the layout, add the method: public void myClickHandler(View view) { // handle click }. Ensure it's public and takes exactly one View parameter.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Replace android:onClick in XML with programmatic setOnClickListener in the activity's onCreate: Button button = findViewById(R.id.button); button.setOnClickListener(v -> myClickHandler(v)); Then define myClickHandler as a void method with View parameter.",
      "success_rate": 0.9,
      "how": "Replace android:onClick in XML with programmatic setOnClickListener in the activity's onCreate: Button button = findViewById(R.id.button); button.setOnClickListener(v -> myClickHandler(v)); Then define myClickHandler as a void method with View parameter.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If using fragments, set the onClick listener in the fragment's onCreateView and delegate to a fragment method. Example: button.setOnClickListener(v -> fragmentMethod(v));",
      "success_rate": 0.85,
      "how": "If using fragments, set the onClick listener in the fragment's onCreateView and delegate to a fragment method. Example: button.setOnClickListener(v -> fragmentMethod(v));",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "In the activity hosting the layout, add the method: public void myClickHandler(View view) { // handle click }. Ensure it's public and takes exactly one View parameter.",
    "Replace android:onClick in XML with programmatic setOnClickListener in the activity's onCreate: Button button = findViewById(R.id.button); button.setOnClickListener(v -> myClickHandler(v)); Then define myClickHandler as a void method with View parameter.",
    "If using fragments, set the onClick listener in the fragment's onCreateView and delegate to a fragment method. Example: button.setOnClickListener(v -> fragmentMethod(v));"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://developer.android.com/reference/android/view/View#setOnClickListener(android.view.View.OnClickListener)",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-09-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}