{"record":{"id":"00bd7779c48d8191","repo":"alibaba/ARouter","slug":"the-fields-need-autowired-from-intent-its-parent","errorCode":null,"errorMessage":"The fields need autowired from intent, its parent must be activity or fragment! [<qualifiedName>]","messagePattern":"The fields need autowired from intent, its parent must be activity or fragment! \\[<qualifiedName>\\]","errorType":"validation","errorClass":"IllegalAccessException","httpStatus":null,"severity":"error","filePath":"arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java","lineNumber":139,"sourceCode":"                for (Element element : childs) {\n                    if (!isSubtypeOf(element.asType(), iProvider)) {\n                        hasAutowiredValues = true;\n                        break;\n                    }\n                }\n\n                if (hasAutowiredValues) {\n                    if (isSubtypeOf(parent.asType(), activityTm)) {\n                        injectMethodBuilder.addStatement(\n                                \"$T bundle = substitute.getIntent() == null ? null : substitute.getIntent().getExtras()\",\n                                AndroidBundle\n                        );\n                    } else if (isSubtypeOf(parent.asType(), fragmentTm)\n                            || isSubtypeOf(parent.asType(), fragmentTmV4)\n                            || isSubtypeOf(parent.asType(), fragmentTmAndroidX)) {\n                        injectMethodBuilder.addStatement(\"$T bundle = substitute.getArguments()\", AndroidBundle);\n                    } else {\n                        throw new IllegalAccessException(\"The fields need autowired from intent, its parent must be activity or fragment! [\"\n                                + parent.getQualifiedName() + \"]\");\n                    }\n                }\n\n                // Generate method body, start inject.\n                for (Element element : childs) {\n                    Autowired fieldConfig = element.getAnnotation(Autowired.class);\n                    String fieldName = element.getSimpleName().toString();\n                    if (isSubtypeOf(element.asType(), iProvider)) {  // It's provider\n                        if (\"\".equals(fieldConfig.name())) {    // User has not set service path, then use byType.\n\n                            // Getter\n                            injectMethodBuilder.addStatement(\n                                    \"substitute.\" + fieldName + \" = $T.getInstance().navigation($T.class)\",\n                                    ARouterClass,\n                                    ClassName.get(element.asType())\n                            );\n                        } else {    // use byName","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/alibaba/ARouter/blob/84f451d244e3fb1d0e37801de1b9ee2af2f81d68/arouter-compiler/src/main/java/com/alibaba/android/arouter/compiler/processor/AutowiredProcessor.java#L121-L157","documentation":"AutowiredProcessor (annotation processor) generates field-injection code only for classes whose parent is an Activity or Fragment. During compilation, if a class annotated for autowiring (with fields marked @Autowired) is neither, the processor throws IllegalAccessException with the class's qualified name, aborting the build.","triggerScenarios":"Adding @Autowired fields to a class that is not an Activity/Fragment (e.g. a Service, BroadcastReceiver, ViewModel, or plain object) while the compiler maven plugin's autowire processing scans it; typically triggered by ARouter's injectable supertype chain.","commonSituations":"Developers assuming @Autowired works in ViewModels or Services; adding ARouter.autoconfigure modules that register non-activity classes; upgrading ARouter and adding @Autowired to newly scanned classes.","solutions":["Move @Autowired fields into Activities or Fragments that ARouter injects","Remove @Autowired from unsupported classes and pass values manually via getIntent/constructor","For Services/others, read extras manually or use ARouter's navigation callback instead of field injection","Check the class hierarchy — the parent chain must bottom out at Activity/Fragment (incl. androidx/support fragments)"],"exampleFix":"// before\npublic class MyViewModel extends ViewModel {\n    @Autowired\n    String userId; // IllegalAccessException at compile time\n}\n// after\npublic class UserActivity extends AppCompatActivity {\n    @Autowired\n    String userId; // injected by ARouter for activity\n    @Override protected void onCreate(Bundle b) {\n        ARouter.getInstance().inject(this);\n    }\n}","handlingStrategy":"validation","validationCode":"// compile-time: only annotate supported parents\n// ensure: activity instanceof Activity || activity instanceof Fragment\nif (!(clazz.isAssignableFrom(Activity.class)) && !(isFragment(clazz))) {\n    throw new IllegalStateException(\"@Autowired only valid on Activity/Fragment: \" + clazz);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only put @Autowired fields on Activity/Fragment subclasses","Remove @Autowired from ViewModels, Services, plain POJOs","Run a quick assembleDebug after adding @Autowired to a new class to fail fast","Keep ARouter compiler plugin and api versions aligned"],"tags":["android","arouter","annotation-processor","compile-time","autowired"],"backgroundTag":"incompatible-source-type","analyzedSha":"84f451d244e3fb1d0e37801de1b9ee2af2f81d68","analyzedAt":"2026-09-06T13:30:41.084Z","contentChangedAt":"2026-09-06T13:30:41.084Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}