Android - 用户设置片段
Android - user setting fragment
我的用户设置片段不起作用
它真的很简单 - 因此它应该可以工作。
菜单用户设置 -> onOptionsItemSelected-> new Intent -> startActivityForResult(intent, key) -> 现在应该使用 fragment.xml.
UserPreferencesFragment.java
public class UserPreferencesFragment extends PreferenceFragment {
private static String SETTINGS_NAME_KEY = "name";
private static String SETTINGS_EMAIL_KEY = "name";
public static String getName(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(SETTINGS_NAME_KEY, "");
}
public static String getEmail(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(SETTINGS_EMAIL_KEY, "");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//adding the preferences from the xml
//so this will in fact be the whole view.
addPreferencesFromResource(R.xml.prefs);
}
}
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".......UserPreferencesActivity">
<LinearLayout android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textStyle="bold"
android:textSize="@dimen/h1"
android:text="@string/user_name" />
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/h1"
android:text="" />
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginRight="10dp"
android:textSize="@dimen/h1"
android:text="@string/user_email" />
<TextView
android:id="@+id/userEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/h1"
android:text="" />
</LinearLayout>
</LinearLayout>
现在一切正常,但我收到了一个完整的异常。
03-26 11:42:58.584 31714-31714/xx.xx.xxxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: xx.xx.xxxx, PID: 31714
java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xx.xxxx/xx.xx.xxxx.UserPreferencesActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:443)
at android.preference.GenericInflater.inflate(GenericInflater.java:318)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:301)
at xx.xx.xxxx.UserPreferencesFragment.onCreate(UserPreferencesFragment.java:41)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[zip file "/data/app/xx.xx.xxxx-2/base.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_dependencies_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_0_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_1_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_2_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_3_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_4_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_5_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_6_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_7_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_8_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/xx.xx.xxxx-2/lib/x86_64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.preference.GenericInflater.createItem(GenericInflater.java:376)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:419)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
at android.preference.GenericInflater.inflate(GenericInflater.java:318)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:301)
at xx.xx.xxxx.UserPreferencesFragment.onCreate(UserPreferencesFragment.java:41)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: android.preference.resources
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 26 more
Caused by: java.lang.NoClassDefFoundError: Class not f
您需要使用:
setContentView(R.layout.fragment);
我认为在尝试添加首选项之前在 onCreate() 中。
希望对您有所帮助
您的 prefs.xml
文件的根布局必须是 <PreferenceScreen>
而不是 <LinearLayout>
,我猜您使用的是错误文件
我的用户设置片段不起作用
它真的很简单 - 因此它应该可以工作。
菜单用户设置 -> onOptionsItemSelected-> new Intent -> startActivityForResult(intent, key) -> 现在应该使用 fragment.xml.
UserPreferencesFragment.java
public class UserPreferencesFragment extends PreferenceFragment { private static String SETTINGS_NAME_KEY = "name"; private static String SETTINGS_EMAIL_KEY = "name"; public static String getName(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getString(SETTINGS_NAME_KEY, ""); } public static String getEmail(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getString(SETTINGS_EMAIL_KEY, ""); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //adding the preferences from the xml //so this will in fact be the whole view. addPreferencesFromResource(R.xml.prefs); } }
XML 文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".......UserPreferencesActivity"> <LinearLayout android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:textStyle="bold" android:textSize="@dimen/h1" android:text="@string/user_name" /> <TextView android:id="@+id/userName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/h1" android:text="" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:layout_marginRight="10dp" android:textSize="@dimen/h1" android:text="@string/user_email" /> <TextView android:id="@+id/userEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/h1" android:text="" /> </LinearLayout> </LinearLayout>
现在一切正常,但我收到了一个完整的异常。
03-26 11:42:58.584 31714-31714/xx.xx.xxxx E/AndroidRuntime: FATAL EXCEPTION: main
Process: xx.xx.xxxx, PID: 31714
java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.xx.xxxx/xx.xx.xxxx.UserPreferencesActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:443)
at android.preference.GenericInflater.inflate(GenericInflater.java:318)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:301)
at xx.xx.xxxx.UserPreferencesFragment.onCreate(UserPreferencesFragment.java:41)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[zip file "/data/app/xx.xx.xxxx-2/base.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_dependencies_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_0_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_1_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_2_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_3_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_4_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_5_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_6_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_7_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_8_apk.apk", zip file "/data/app/xx.xx.xxxx-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/xx.xx.xxxx-2/lib/x86_64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.preference.GenericInflater.createItem(GenericInflater.java:376)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:419)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
at android.preference.GenericInflater.inflate(GenericInflater.java:318)
at android.preference.GenericInflater.inflate(GenericInflater.java:264)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:273)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:301)
at xx.xx.xxxx.UserPreferencesFragment.onCreate(UserPreferencesFragment.java:41)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: android.preference.resources
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 26 more
Caused by: java.lang.NoClassDefFoundError: Class not f
您需要使用:
setContentView(R.layout.fragment);
我认为在尝试添加首选项之前在 onCreate() 中。
希望对您有所帮助
您的 prefs.xml
文件的根布局必须是 <PreferenceScreen>
而不是 <LinearLayout>
,我猜您使用的是错误文件