我的应用包括同步适配器; 多年来一直运作良好,但最近我收到了一份非常奇怪的崩溃报告:
interface Document {
mozCancelFullScreen: () => void;
mozFullScreenElement: Element;
}
ScormApplication.java:
Samsung Galaxy S6 Edge+ (zenlte), 4096MB RAM, Android 7.0
java.lang.ClassCastException:
1. at com.myappid.android.ScormApplication.getApplication (ScormApplication.java:34)
2. at com.myappid.android.sync.SyncAdapter.onPerformSync (SyncAdapter.java:98)
3. at android.content.AbstractThreadedSyncAdapter$SyncThread.run (AbstractThreadedSyncAdapter.java:272)
SyncAdapter.java:
public class ScormApplication extends Application {
//...
public static ScormApplication getApplication(Context context) {
if (context instanceof ScormApplication) {
return (ScormApplication) context;
}
return (ScormApplication) context.getApplicationContext(); // this is a line 34
}
//...
}
因此,这意味着调用SyncAdapter上下文的方法public class SyncAdapter extends AbstractThreadedSyncAdapter {
//...
@Override
public void onPerformSync(Account account,
Bundle extras,
String authority,
ContentProviderClient provider,
SyncResult syncResult) {
// sync code here...
ScormApplication.getApplication(getContext()).sendOttoEvent(ottoEvent); // this is a line 98
}
}
中返回的对象不是我的应用程序类的实例。但是怎么可能呢?
以下是我的代码:
AndroidManifest.xml (未声明单独的流程) :
getApplicationContext
SyncService.java:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ...>
<application ...>
...
<service
android:name="com.myappid.android.sync.SyncService"
android:exported="true">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
...
@ XML / syncadapter:
public class SyncService extends Service {
private static SyncAdapter sSyncAdapter = null;
private static final Object sSyncAdapterLock = new Object();
@Override
public void onCreate() {
synchronized (sSyncAdapterLock) {
if (sSyncAdapter == null) {
sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
}
}
}
@Override
public IBinder onBind(Intent intent) {
return sSyncAdapter.getSyncAdapterBinder();
}
}