android - ClassCastException when I load components from different apk by DexClassLoader -
i have application (main app) shows data recyclerview , cardview. got show same ui on different application (test app), , dexclassloader avoid duplicated code. (i can't use main app's activity reason)
first of all, main app loaded test app
classloader loader = new dexclassloader(path, getcodecachedir().getabsolutepath(), null, getclassloader()); try { class<?> fragmentcls = loader.loadclass("com.example.mainapp.recyclerlistfragment"); method method = fragmentcls.getmethod("newinstance", null); object object = method.invoke(fragmentcls, null); fragment fragment = (fragment) object; fragmentmanager fragmentmanager = getfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.activity_main, fragment); fragmenttransaction.commit(); } catch (exception e) { e.printstacktrace(); } then if try main app's resource test app's context, resources$notfoundexception. create main app's context access main app's resource. example, in main app's code
context ctx = context.createpackagecontext(pkg_name, context.context_include_code | context.context_ignore_security); view = layoutinflater.from(ctx).inflate(r.layout.cardview_item, parent, false); but when try find customized view extends android.support.v7.widget.cardview,
mycardview = itemview.findviewbyid(r.id.card_view); it shows classcastexception.
androidruntime: java.lang.classcastexception: com.example.mainapp.view.extcardview cannot cast com.example.mainapp.view.extcardview i have searched related information time, people mention caused same class loaded default classloader , new dexclassloader, reference.
com.example.mainapp.view.extcardview exists in main app, not in test app. means test app's default loader should not load class itself. test app doesn't use android.support.v7.widget.cardview.
does has ideas classcastexception?
Comments
Post a Comment