我注意到,在某些设备上,我总是NullPointerException
在打电话时得到DbxAccountManager.getInstance
,但大多数时间和大多数设备上的代码都可以正常工作。
这是我基本上在做的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//some initialization
mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}
哪里DbxAccountManager.getInstance
调用 Dropbox 的 Sync API SDK。
这是跟踪:
04-20 22:36:52.565: E/AndroidRuntime(551): Uncaught handler: thread main exiting due to uncaught exception
04-20 22:36:52.575: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nobeid.XXX/com.nobeid.XXX.Settings}: java.lang.NullPointerException
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.os.Looper.loop(Looper.java:123)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread.main(ActivityThread.java:4203)
04-20 22:36:52.575: E/AndroidRuntime(551): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 22:36:52.575: E/AndroidRuntime(551): at java.lang.reflect.Method.invoke(Method.java:521)
04-20 22:36:52.575: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-20 22:36:52.575: E/AndroidRuntime(551): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
04-20 22:36:52.575: E/AndroidRuntime(551): at dalvik.system.NativeStart.main(Native Method)
04-20 22:36:52.575: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
04-20 22:36:52.575: E/AndroidRuntime(551): at com.dropbox.sync.android.DbxAccountManager.validateAppContext(DbxAccountManager.java:432)
04-20 22:36:52.575: E/AndroidRuntime(551): at com.dropbox.sync.android.DbxAccountManager.getInstance(DbxAccountManager.java:106)
04-20 22:36:52.575: E/AndroidRuntime(551): at com.dropbox.sync.android.DbxAccountManager.getInstance(DbxAccountManager.java:99)
04-20 22:36:52.575: E/AndroidRuntime(551): at com.nobeid.XXX.Settings.onCreate(Settings.java:198)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
04-20 22:36:52.575: E/AndroidRuntime(551): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
04-20 22:36:52.575: E/AndroidRuntime(551): ... 11 more
有谁知道如何解决这个问题。任何线程都会有所帮助。
编辑1:
getInstance
这是从 Dropbox 的 SDK中获取的DbxAccountManager
static DbxAccountManager getInstance(Context applicationContext, DbxConfig config) {
if (null == applicationContext) throw new NullPointerException("applicationContext shouldn't be null.");
if (null == config) throw new NullPointerException("config shouldn't be null.");
AppConfigStrings appStrings = validateAppContext(applicationContext, config);
CoreConfig coreConfig = new CoreConfig(config, CoreConfig.Hosts.DEFAULT, appStrings.userAgent, appStrings.appVersion, appStrings.deviceId);
synchronized (sInitializeLock) {
if (null == sInstance) {
sInstance = new DbxAccountManager(new CoreAccountManager(applicationContext, coreConfig));
}
else if (!config.equals(sInstance.mAcctMgr.getConfig())) {
throw new ConfigurationMismatchException("Dropbox.ensureInitialized called with different configuration.");
}
return sInstance;
}
}
和validateAppContext
:
private static AppConfigStrings validateAppContext(Context appContext, DbxConfig config)
{
Context testContext = appContext.getApplicationContext();
if (testContext != appContext) {
throw new IllegalArgumentException("The provided context wasn't an application context.");
}
PackageInfo pi = null;
try
{
PackageManager pm = appContext.getPackageManager();
pi = pm.getPackageInfo(appContext.getPackageName(), 4101);
}
catch (PackageManager.NameNotFoundException e)
{
CoreLogger.getGlobal().logAndThrow(TAG, new RuntimeException("Unable to get package info for app package.", e));
}
CoreAssert.isTrue(null != pi);
Set requiredActivities = new HashSet();
requiredActivities.add(DbxAuthActivity.class.getName());
requiredActivities.add(AuthActivity.class.getName());
for (ActivityInfo info : pi.activities) {
requiredActivities.remove(info.name);
}
if (!requiredActivities.isEmpty()) {
throw new IllegalStateException("Required Sync API Activity isn't included in application manifest: " + TextUtils.join(", ", requiredActivities));
}
Set requiredServices = new HashSet();
requiredServices.add(DbxSyncService.class.getName());
for (ServiceInfo info : pi.services) {
requiredServices.remove(info.name);
}
if (!requiredServices.isEmpty()) {
throw new IllegalStateException("Required Sync API Service isn't included in application manifest: " + TextUtils.join(", ", requiredServices));
}
Set requiredPermissions = new HashSet();
requiredPermissions.add("android.permission.INTERNET");
requiredPermissions.add("android.permission.ACCESS_NETWORK_STATE");
for (String perm : pi.requestedPermissions) {
requiredPermissions.remove(perm);
}
if (!requiredPermissions.isEmpty()) {
throw new IllegalStateException("Required Sync API permission isn't requested in application manifest: " + TextUtils.join(", ", requiredPermissions));
}
AuthActivity.checkAppBeforeAuth(appContext, config.appKey, false);
String locale = Locale.getDefault().toString();
StringBuilder sbUserAgent = new StringBuilder();
sbUserAgent.append(appContext.getPackageName()).append('/').append(pi.versionCode);
sbUserAgent.append(' ').append("DropboxSync/").append(SDK_VERSION_NAME);
sbUserAgent.append(" (Android; ").append(CoreAndroidUtil.getSystemVersion()).append("; ").append(Build.MANUFACTURER).append(" ").append(Build.MODEL).append(" ").append(Build.CPU_ABI).append("; ").append(locale).append(")");
StringBuilder sbAppVersion = new StringBuilder();
sbAppVersion.append(pi.versionCode).append(' ');
sbAppVersion.append("Dropbox-Sync-Sdk-Android/").append(SDK_VERSION_NAME);
return new AppConfigStrings(sbUserAgent.toString(), sbAppVersion.toString(), CoreAndroidUtil.getDeviceId(appContext));
}
第 432 行是:requiredServices.remove(info.name);
编辑2: 从问题中删除了不相关(误导)的东西。(感谢CommonsWare)