如何通过方法调用从对象强制转换类(ClassCastException)

时间:2018-05-09 01:17:15

标签: java android

我有Main apk和Sub apk(实际上是sub apk是一种图书馆)
并且它们都有相同的arr文件库

主应用程序通过dexclassloader从子应用程序加载“MyClassInterface”类 因为主应用程序在assets文件夹中有子apk文件

“MyClassInterface”类的“getMyClassInstance”方法通过方法调用返回MyClass实例
所以,我想要的是

MyClass myClass = (MyClass) methodGetMyClass.invoke(object);
myClass.getName();

但是ClassCastExetion在这里跟随日志
这不可能吗?如果没有,我怎么能上课?

System.err java.lang.ClassCastException:com.example.library.MyClass cannot be cast to com.example.libary.MyClass
System.err at com.example.woosuk.mainapplication.MainActivity.onCreate(MainActivity.java:34)
System.err at android.app.Activity.performCreate(Activity.java:7174)
System.err at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
System.err at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
System.err at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
System.err at android.app.ActivityThread.-wrap11(Unknown Source:0)
System.err at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
System.err at android.os.Handler.dispatchMessage(Handler.java:105)
System.err at android.os.Looper.loop(Looper.java:164)
System.err at android.app.ActivityThread.main(ActivityThread.java:6938)
System.err at java.lang.reflect.Method.invoke(Native Method)
System.err at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
System.err at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

--------------------主要应用------------------------- ----

DexClassLoader dexClassLoader = new DexClassLoader("data/data/com.example.woosuk.mainapplication/sub.apk", "", null, ClassLoader.getSystemClassLoader());
try {
    Class<?> _class = dexClassLoader.loadClass("com.example.woosuk.subapplication.MyClassInterface");
    Object object = _class.newInstance();

    Method methodGetIntValue = _class.getMethod("getIntValue");
    Log.e("TEST", "getIntValue return " + methodGetIntValue.invoke(object));//working fine

    Method methodGetMyClass = _class.getMethod("getMyClassInstance");
    MyClass myClass = (MyClass) methodMyClass.invoke(object);//ClassCastException!!!!!
} catch (Exception e) {
    e.printStackTrace();
}

-------------------- sub app ------------------------- ------

public class MyClassInterface {

    public MyClass getMyClassInstance() {
        return new MyClass(99, "test");
    }

    public int getIntValue() {
        return 99;
    }
}

---------------------图书馆的MyClass --------------

package com.example.library;
public class MyClass {

    private int version;
    private String name;

    public MyClass(int version, String name) {
        this.version = version;
        this.name = name;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Github:https://github.com/woosuk0410

图书馆:https://github.com/woosuk0410/Library

SubApp:https://github.com/woosuk0410/SubApplication

MainApp:https://github.com/woosuk0410/MainApplication

how to cast class from method invoke by dexclassloader ( ClassCastException )

相同的排名

1 个答案:

答案 0 :(得分:1)

由于你没有使用相同的ClassLoader来加载这两个类。

请尝试使用MyClass.class.getClassLoader()代替ClassLoader.getSystemClassLoader()