2

我正在运行 Android Studio 2.0 版的新稳定版本。当我禁用即时运行时,我的应用程序运行良好,但是当我打开它时,它给了我这个错误:

Caused by: java.lang.ClassCastException: com.android.tools.fd.runtime.BootstrapApplication cannot be cast to com.my.app.CustomApplication

CustomApplication 是我通过上下文获得的应用程序类。但我似乎无法得到它。当即时运行开启时,我的类被转换为 BootstrapApplication 然后失败。

我的应用是类似于 FB 聊天头的浮动服务。

我有最新的 gradle 版本:

classpath 'com.android.tools.build:gradle:2.0.0'

这里的其他答案说 Instant Run 尝试对代码进行热交换;这会导致应用程序类被移动。

那么我该如何解决呢?

4

2 回答 2

6

解决方案 #1 -在设置中禁用即时运行

解决方案 #2 - 使用反射从 BootstrapApplication 获取真正的应用程序

public static CustomApplication getRealApplication (Context applicationContext)
{
    CustomApplication application = null;

    if (applicationContext instanceof CustomApplication)
    {
        application = (CustomApplication) applicationContext;
    }
    else
    {
        Application realApplication = null;
        Field magicField = null;
        try
        {
            magicField = applicationContext.getClass().getDeclaredField("realApplication");
            magicField.setAccessible(true);
            realApplication = (Application) magicField.get(applicationContext);
        }
        catch (NoSuchFieldException e)
        {
            Log.e(TAG, e.getMessage());
        }
        catch (IllegalAccessException e)
        {
            Log.e(TAG, e.getMessage());
        }

        application = (CustomApplication) realApplication;
    }

    return application;
}

在某处使用:

    Context applicationContext = getContext().getApplicationContext();
    CustomApplication application = getRealApplication(applicationContext);

使用示例:

public class MyProvider extends OrmLiteProvider<OrmLiteSqliteOpenHelper, OrmLiteUriMatcher<OrmLiteMatcherEntry>>
{
    @Override
    protected OrmLiteSqliteOpenHelper createHelper ()
    {
        Context applicationContext = getContext().getApplicationContext();
        CustomApplication application = CustomApplication.getRealApplication(applicationContext);
        return application.getComponent().databaseHelper();
    }

   ...

}
于 2016-04-12T14:18:15.730 回答
0

public static CustomApplication getRealApplication (Context applicationContext)
{
    CustomApplication application = null;

    if (applicationContext instanceof CustomApplication)
    {
        application = (CustomApplication) applicationContext;
    } else if (applicationContext.getApplicationContext() instanceof CustomApplication) {
        application = (CustomApplication) applicationContext.getApplicationContext() ;
    }
    else
    {
        Application realApplication = null;
        Field magicField = null;
        try
        {
            magicField = applicationContext.getClass().getDeclaredField("realApplication");
            magicField.setAccessible(true);
            realApplication = (Application) magicField.get(applicationContext);
        }
        catch (NoSuchFieldException e)
        {
            Log.e(TAG, e.getMessage());
        }
        catch (IllegalAccessException e)
        {
            Log.e(TAG, e.getMessage());
        }

        application = (CustomApplication) realApplication;
    }

    return application;
}

在解决方案#2中,我添加了一种情况

于 2017-08-11T03:53:42.700 回答