Android - 用户设置片段

时间:2018-03-26 12:53:27

标签: android android-fragments setting

我的用户设置片段不起作用

这很简单 - 因此它应该有效。

菜单用户设置 - > onOptionsItemSelected->新意图 - > 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

2 个答案:

答案 0 :(得分:0)

您需要使用:

setContentView(R.layout.fragment);
在你尝试添加你的prefs之前,在onCreate()中

希望这有帮助

答案 1 :(得分:0)

prefs.xml文件的根布局必须是<PreferenceScreen>而不是<LinearLayout>,我猜你使用了错误的文件