Sugar ORM:尝试从getApplicationContext()转换上下文时的类强制转换异常

时间:2015-10-21 09:41:04

标签: java android classcastexception sugarorm

我试图将一个上下文转换为一个Activity。我需要这样做来调用该活动的方法之一。为了获得我需要的上下文,我使用了getApplicationContext()。我正在开发的应用程序使用Sugar ORM作为数据库框架。这是我得到的代码和例外。

ContentResolver.requestSync(((MainActivity) getApplicationContext()).getAccount(), AccountHelper.AUTHORITY, settingsBundle);

java.lang.ClassCastException: com.orm.SugarApp cannot be cast to id.ac.ui.presentronik.activity.MainActivity
        at id.ac.ui.presentronik.activity.SessionActivity.onOptionsItemSelected(SessionActivity.java:173)
        at android.app.Activity.onMenuItemSelected(Activity.java:2885)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350)
        at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155)
        at android.support.v7.app.ActionBarActivityDelegate$1.onMenuItemSelected(ActionBarActivityDelegate.java:74)
        at android.support.v7.widget.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:44)
        at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:77)
        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:163)
        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:738)
        at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
        at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
        at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
        at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:596)
        at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:145)
        at android.view.View.performClick(View.java:4856)
        at android.view.View$PerformClick.run(View.java:19956)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5371)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:945)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:740)

我遇到的问题似乎与此问题中的问题相同:SugarORM in AndroidManifest。但该解决方案仅适用于片段。有没有其他方法可以从活动中获取正确的上下文?

1 个答案:

答案 0 :(得分:0)

  

com.orm.SugarApp无法强制转换为   id.ac.ui.presentronik.activity.MainActivity

将getActivity()作为上下文传递:

ContentResolver.requestSync(getActivity().getAccount(), AccountHelper.AUTHORITY, settingsBundle);

OR

ContentResolver.requestSync(((com.orm.SugarApp) getApplicationContext()).getAccount(), AccountHelper.AUTHORITY, settingsBundle);

请记住,在你的清单中你设置了这样的东西:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    tools:replace="android:icon"
    android:name="com.orm.SugarApp">

您还需要使用这些权限:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />