1
我試圖將一個上下文轉換爲一個Activity。我需要這樣做來調用其中一個活動的方法。爲了獲得我需要的上下文,我使用了getApplicationContext()。我現在正在開發的應用程序使用Sugar ORM作爲數據庫框架。這是我得到的代碼和例外。Sugar ORM:嘗試從getApplicationContext()轉換上下文時出現類轉換異常
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。但該解決方案僅適用於碎片。有沒有其他方法可以從活動中獲得正確的上下文?
這個面臨同樣的問題在這裏的任何解決方案.. –