本文介绍了铸造的Class.forName对象C​​lassCastException异常时,接口由该对象实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建Android的一些插件的例子,所以我有3个项目吧:

I want to create some plugin example for android, so I have 3 projects for it:

ExternalLibInterface 的 - 包含 IExternalLib ,并建立以 externallibinterface.jar 文件

ExternalLibInterface - contains IExternalLib, and builds to externallibinterface.jar file

  package com.example.externallibinterface;
  public interface IExternalLib {
    public String someMethod( String param );
  }

ExternalLib 的 - 包含 externallibinterface.jar SomeClass的实现IExternalLib ,建立以 externallib.apk

ExternalLib - contains externallibinterface.jar and SomeClass implements IExternalLib, builds to externallib.apk

   package com.example.externallib;
   import com.example.externallibinterface.IExternalLib;
   public class SomeClass implements IExternalLib {
       public String someMethod(String arg0) {
           return arg0;
       }
   }

SomeApp 的 - 包含 externallibinterface.jar 和类活动 - 应用程序,我加载外部APK和类从它

SomeApp - contains externallibinterface.jar and class for activity - application where I load external apk and class from it.

   import com.example.externallibinterface.IExternalLib;
   import dalvik.system.PathClassLoader;

   public class MainActivity extends Activity {

       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           String apkToLoad = null;

           String externalPackagePath = "com.example.externallib";
           String externalClassPath = "com.example.externallib.SomeClass";

           try {
               apkToLoad = getPackageManager()
                    .getApplicationInfo( externalPackagePath, MODE_PRIVATE ).sourceDir;
           } catch ( PackageManager.NameNotFoundException e ) {
               e.printStackTrace();
           }

           PathClassLoader pathClassLoader =
                   new PathClassLoader( apkToLoad,
                ClassLoader.getSystemClassLoader() );

           try {
               Class<?> c = Class.forName( externalClassPath, true, pathClassLoader );

               Object someClassInstance = c.newInstance();
                       //exception ClassCastException here
               IExternalLib i = (IExternalLib) someClassInstance;
               i.someMethod( "some string" );
           } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
           } catch (InstantiationException e) {
            e.printStackTrace();
           } catch (IllegalAccessException e) {
            e.printStackTrace();
           } catch ( ClassCastException e ) {
            e.printStackTrace();
           }
       }
   }

但是,当我投对象someClassInstance IExternalLib 我得到 ClassCastException异常
为什么? IExternalLib 在第3个地方被定义(在 externallibinterface.jar )。

But when I cast Object someClassInstance to IExternalLib I get ClassCastException.Why? IExternalLib is defined in 3rd place (in externallibinterface.jar).

推荐答案

在不同的类加载器加载的类这可能发生。确保一次构建整个设置,以便只有一个类加载器是负责加载类。它经常发生,当你只是重新部署一个特定的.war文件到现有的系统。请参阅本作更深入的了解不能转换到实现的接口

It could happen when different class loaders are loading the class. Make sure to build the whole setup at once so that only one class loader is responsible for loading the classes. It happens frequently when you are just re-deploying a particular .war file onto a existing system. Please see this for more insight can't cast to implemented interface

这篇关于铸造的Class.forName对象C​​lassCastException异常时,接口由该对象实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!