我实施了一个简单的应用程序,通过AccountManager,可以创建新用户并转到"设置 - >帐户" (从系统配置中)您可以配置/删除已创建的帐户。
这是我的 AndroidManifest.xml:
的一部分<service android:name="pratiche.auth.CustomAuthService"
android:exported="false" >
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/custom_authenticator" />
</service>
<activity android:name="pratiche.auth.SettingsActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
custom_authenticator.xml
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="pratiche.android.account" android:icon="@mipmap/ic_launcher"
android:label="@string/custom_authenticator_label"
android:accountPreferences="@xml/pref_general"/>
pref_general.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference android:defaultValue="true" android:key="example_switch"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />
SettingsActivity.java:
public class SettingsActivity extends PreferenceActivity {
/**
* Helper method to determine if the device has an extra-large screen. For
* example, 10" tablets are extra-large.
*/
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* {@inheritDoc}
*/
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}
/**
* {@inheritDoc}
*/
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<PreferenceActivity.Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
/**
* This method stops fragment injection in malicious applications.
* Make sure to deny any unknown fragments here.
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName);
}
/**
* This fragment shows general preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
pref_headers.xml:
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header
android:fragment="pratiche.auth.SettingsActivity$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/pref_header_general" />
</preference-headers>
gradle配置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "pratiche"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:preference-v7:24.2.1'
compile 'com.android.support:preference-v14:24.2.1'
testCompile 'junit:junit:4.12'
}
问题是在使用Android Marshmallow(6+)的Nexus 5x上执行的相同代码正常运行: created account, account settings
使用Android Nougat(7+)在Nexus 5x上运行不起作用并返回以下错误:
java.lang.RuntimeException: Unable to resume activity {com.android.settings/com.android.settings.SubSettings}: android.view.InflateException: Binary XML file line #3: Error inflating class (not found)SwitchPreference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3400)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class (not found)SwitchPreference
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:301)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:122)
at com.android.settings.accounts.AccountPreferenceBase.addPreferencesForType(AccountPreferenceBase.java:161)
at com.android.settings.accounts.ManageAccountsSettings.addPreferencesForType(ManageAccountsSettings.java)
at com.android.settings.accounts.ManageAccountsSettings.addAuthenticatorSettings(ManageAccountsSettings.java:406)
at com.android.settings.accounts.ManageAccountsSettings.showAccountsIfNeeded(ManageAccountsSettings.java:398)
at com.android.settings.accounts.ManageAccountsSettings.onResume(ManageAccountsSettings.java:112)
at android.app.Fragment.performResume(Fragment.java:2398)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2049)
at android.app.FragmentController.dispatchResume(FragmentController.java:198)
at android.app.Activity.performResume(Activity.java:6789)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.preference.SwitchPreference" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/CellBroadcastReceiver/lib/arm64, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:233)
at android.support.v7.preference.PreferenceInflater.onCreateItem(PreferenceInflater.java:281)
at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:290)
at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:359)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:167)
at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:117)
at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:122)
at com.android.settings.accounts.AccountPreferenceBase.addPreferencesForType(AccountPreferenceBase.java:161)
at com.android.settings.accounts.ManageAccountsSettings.addPreferencesForType(ManageAccountsSettings.java)
at com.android.settings.accounts.ManageAccountsSettings.addAuthenticatorSettings(ManageAccountsSettings.java:406)
at com.android.settings.accounts.ManageAccountsSettings.showAccountsIfNeeded(ManageAccountsSettings.java:398)
at com.android.settings.accounts.ManageAccountsSettings.onResume(ManageAccountsSettings.java:112)
at android.app.Fragment.performResume(Fragment.java:2398)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2049)
at android.app.FragmentController.dispatchResume(FragmentController.java:198)
at android.app.Activity.performResume(Activity.java:6789)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3377)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2713)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
谁能告诉我什么是错的?