- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在实现具有虚拟帐户和虚拟帐户身份 validator 的 Syncadapter 时遇到问题。
我的帐户 validator ,CNAuthenticator:
public class CNAuthenticator extends AbstractAccountAuthenticator {
public CNAuthenticator(Context context) {
super(context);
}
@Override
public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
return null;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public String getAuthTokenLabel(String authTokenType) {
return null;
}
@Override
public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
return null;
}
@Override
public Bundle hasFeatures(AccountAuthenticatorResponse response, Account account, String[] features) throws NetworkErrorException {
return null;
}
}
我的身份验证服务 CNAuthenticatorService:
public class CNAuthenticatorService extends Service {
private CNAuthenticator cnAuthenticator;
public CNAuthenticatorService() {
}
@Override
public void onCreate() {
super.onCreate();
cnAuthenticator= new CNAuthenticator(this);
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return cnAuthenticator.getIBinder();
}
}
我的同步适配器,CNSyncAdapter:
public class CNSyncAdapter extends AbstractThreadedSyncAdapter {
private static final String LOG_TAG = "CNSyncAdapter";
public CNSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(LOG_TAG, "onPerformSync Called.");
}
public static Account getSyncAccount(Context context) {
// Get an instance of the Android account manager
AccountManager accountManager =(AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
// Create the account type and default account
Account newAccount = new Account(context.getString(R.string.app_name), context.getString(R.string.sync_account_type));
// If the password doesn't exist, the account doesn't exist
if ( null == accountManager.getPassword(newAccount) ) {
/*
* Add the account and account type, no password or user data
* If successful, return the Account object, otherwise report an error.
*/
if (!accountManager.addAccountExplicitly(newAccount, null, null)) {
return null;
}
/*
* If you don't set android:syncable="true" in
* in your <provider> element in the manifest,
* then call ContentResolver.setIsSyncable(account, AUTHORITY, 1)
* here.
*/
}
return newAccount;
}
public static void syncImmediately(Context context) {
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(getSyncAccount(context),
context.getString(R.string.content_authority), bundle);
}
public static void initializeSyncAdapter(Context context) {
getSyncAccount(context);
}
}
我的同步适配器服务,CNSyncService:
public class CNSyncService extends Service {
private static final Object sSyncAdapterLock = new Object();
private static final String LOG_TAG ="CNSyncService" ;
private static CNSyncAdapter cnSyncAdapter = null;
public CNSyncService() {
Log.d(LOG_TAG, "onCreate");
synchronized (sSyncAdapterLock) {
if (cnSyncAdapter==null)
cnSyncAdapter = new CNSyncAdapter(getBaseContext(),true);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return cnSyncAdapter.getSyncAdapterBinder();
}
}
当我在一个 fragment 中调用 CNSyncAdapter.syncImmediately(getActivity())
时出现异常。
我的日志猫,
06-22 19:48:35.080 27790-28417/com.mobileapplicationsclub.cashnote.app W/dalvikvm﹕ threadid=17: thread exiting with uncaught exception (group=0x41e0c2b8)
06-22 19:48:35.120 27790-28417/com.mobileapplicationsclub.cashnote.app E/AndroidRuntime﹕ FATAL EXCEPTION: SyncAdapterThread-1
java.lang.NullPointerException
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:252)
请帮忙。我不知道是什么原因造成的。
这很奇怪,但是当我从 getBaseContext()
更改为 getApplicationContext()
时,我收到了不同的错误。
更改后我的 logcat:
06-22 20:21:00.630 29104-29104/com.mobileapplicationsclub.cashnote.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate service com.mobileapplicationsclub.cashnote.app.CNSyncService: java.lang.NullPointerException
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2406)
at android.app.ActivityThread.access$1700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:4952)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
at com.mobileapplicationsclub.cashnote.app.CNSyncService.<init>(CNSyncService.java:17)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2403)
CNSyncService.java:17 是 cnSyncAdapter = new CNSyncAdapter(getApplicationContext(),true)
最佳答案
您应该使用服务构造函数。当您调用 startService
时,Android 会隐式执行此操作。您可以使用 onCreate
回调方法来实现您的自定义代码:
public class CNSyncService extends Service {
private static final Object sSyncAdapterLock = new Object();
private static final String LOG_TAG ="CNSyncService" ;
private static CNSyncAdapter cnSyncAdapter = null;
@Override
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "onCreate");
synchronized (sSyncAdapterLock) {
if (cnSyncAdapter==null)
cnSyncAdapter = new CNSyncAdapter(getBaseContext(),true);
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return cnSyncAdapter.getSyncAdapterBinder();
}
}
关于java - "SyncAdapter"执行过程中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30983344/
我正在通过 SyncAdapter onPerformSync 发送几个改造调用,并且我试图通过通过 try/catch sleep 语句发送出去来调节 http 调用。但是,这会阻塞 UI,并且只有
如果账户 A 的 SyncAdapter A 在 ContactsContract.RawContacts 表中创建了原始联系人,那么账户 B 的 SyncAdapter B 是否可以更新 SyncA
Android SyncAdapter 不考虑主线程设置的静态变量值。由于静态变量在应用程序中持续存在,因此预计可以跨线程共享。但 SyncAdapter 不考虑主线程设置的那些值并采用默认值。帮助我
我在实现具有虚拟帐户和虚拟帐户身份 validator 的 Syncadapter 时遇到问题。 我的帐户 validator ,CNAuthenticator: public class CNAut
如果我通过“设置/帐户”屏幕选中复选框,我就会运行自动同步。如果我将 syncadapter.xml 中的 UserVisible 设置为 false,则无法选中该复选框。如何在没有设置屏幕选项的情况
我实现了一个简单的 syncadapter,它在 Moto G 上运行良好,但在 Nexus 7 上却不行(永远不会调用 onPerformSync) 为了测试,我像这样手动启动同步: Bundle
是否可以在应用程序未运行时在后台运行 SyncAdapter?如果服务器有任何变化,我想更新 sqlite 数据库,即使 android 应用程序没有运行? 最佳答案 这个用例的最佳实践可能是: 1)
如何从 Sync Adapter 服务(标红)获取最后更新日期? 最佳答案 很有趣。 API 将(几乎必须)在 android.content.ContentResolver 上,因为这是管理定期同步
我有一个应用程序,它使用 SyncAdapter 对远程服务器进行 REST 调用,然后使用内容提供程序将更新保存到本地 SQLLite 数据库。 我可以通过转到“帐户与同步”来触发调用,然后选择我的
我已经阅读了有关同步适配器的所有详细信息,并且我了解它是如何工作的,但是我找不到任何关于是否可以不让用户取消同步数据的信息。 例如,如果我使用 syncadapter 将我的应用程序的所有数据与服务器
我知道内容修改后 30 秒会自动同步。观察 logcat for fat 的时间更长,这证明自动同步没有发生。至少,不适用于我的自定义 SyncAdapter。 用户提供者: 用户同步适配器: 启
我已经尝试了很棒的谷歌示例来同步来自网络服务的联系人并且工作正常。这称为 SampleSyncAdapter,非常值得:http://developer.android.com/resources/s
我正在尝试编写一个 Android SyncAdapter 并且有点困惑。在同步期间(即在 onPerformSync() 中),我希望能够从远程服务器拉下数据,但也希望将数据推送到远程服务器(这是双
我们的 SyncAdapter 实现不与 android 版本 4.4.4 同步。适用于较低 API 的设备。 我们的应用使用 AbstractThreadedSyncAdapter与服务器通信。我已
我正在编写一个 Android Sync Adapter 并且基本上在无限循环中同步时遇到问题。一旦同步完成,它就会重新开始。 谢谢, 问候, 阿克谢 @Override public voi
我是 syncAdapter 的新手。我看过google IO video (讲座演示 here),我已经阅读了 2 个教程(here 和 here)。 我还尝试了教程的代码,但令人沮丧的是我无法弄清
我已经按照 SDK 中的 SimpleSyncAdapter 示例项目实现了 SyncAdapter、AccountManager 和私有(private) ContentProvider。一切正常。
我的应用程序允许人们使用和管理他们的数据,无论他们是否登录。尽管如此,由于我唯一地标识每台设备,我也想同步来 self 的匿名用户(未登录的用户)的数据。因此,我想知道在这种情况下创建匿名帐户是否是一
我正在尝试为我的 Android 应用程序创建一个 SyncAdapter,以显示来自一个特定 channel 的 YouTube 视频。这些视频是公共(public)领域的,所以我不希望用户登录、创
我目前正在开发一个依赖 SyncAdapter 从服务器刷新其内容的 android 应用程序。我基本上遵循了这些说明:https://developer.android.com/training/s
我是一名优秀的程序员,十分优秀!