Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

How to use preferences in Android support library v7 rev23?

In my app using support library v7 I want to add a preference screen. Since Google provides absolutely no documentation on that subject I have looked up a post found elsewhere here on Stack Overflow.

So this is my project:

activity_preferences.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/toolbar_default"/>

    <fragment
        android:name=".FragmentPreferences"
        name=".FragmentPreferences"
        android:tag=".FragmentPreferences"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

ActivityPreferences.java

package com.example.testandroidsupportv7;

import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class ActivityPreferences extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);
    }

}

FragmentPreferences.java

package com.example.testandroidsupportv7;

import com.example.testandroidsupportv7.R;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;

public class FragmentPreferences extends PreferenceFragmentCompat
{

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.preferences);
    }

}

AndroidManifest.xml

<activity
        android:name=".ActivityPreferences"
        android:label="@string/app_name"
        android:theme="@style/MyTheme.NoActionBar" >
</activity>

xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.v7.preference.PreferenceCategory
        android:title="Splo?ne nastavitve" >

        <android.support.v7.preference.CheckBoxPreference
            android:key="PREFERENCE_KEY_CHECK_BOX"
            android:defaultValue="false"
            android:summaryOff="Value is OFF."
            android:summaryOn="Value is ON."
            android:title="Check box preference" />

    </android.support.v7.preference.PreferenceCategory>

</android.support.v7.preference.PreferenceScreen>

If I try to start this preference activity I get an exception.

logcat

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
FATAL EXCEPTION: main
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
    at com.example.testandroidsupportv7.ActivityPreferences.onCreate(ActivityPreferences.java:13)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.testandroidsupportv7-2.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
    ... 20 more
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)

What is wrong with this code? This should work. The exception given is puzzling and offers no clue. Google provides no documentation and no working example. What I find suspicious is the line:

Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.testandroidsupportv7-2.apk]

How to get rid of this exception?

UPDATE

I am running on android 2.3.5 device. I have added preference.xml

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your logcat hints at the error: XML file line #11: Error inflating class fragment -- the suspicious line there seems to be name=".FragmentPreferences" which you probably wanted to be android:name=".FragmentPreferences". Since you already have that, just delete the erroneous line :)

<fragment
    android:name=".FragmentPreferences"
    android:tag=".FragmentPreferences"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

UPDATE: if that doesn't work, then the other blatantly obvious error is in the android:namevalue. It should be:

android:name="com.example.testandroidsupportv7.FragmentPreferences"

or whatever is YOUR actual path to the FragmentPreferences class.

I even built your project on my end, it took all of less than 5 minutes to create, test, and confirm the error; I got exactly the same error as you, when I fixed android:name it builds just fine. Screenshot:

enter image description here

I hope you will apologize to everyone for your behaviour... it is NOT how to go about getting help from people on SO.

UPDATE 2: partial logcat from my build, with the erroneous android:name value:

11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: FATAL EXCEPTION: main
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: Process: com.example.testandroidsupportv7, PID: 26456
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityPreferences}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:155)
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
...
11-07 14:00:24.742 26456-26456/com.example.testandroidsupportv7 E/AndroidRuntime:   Suppressed: java.lang.ClassNotFoundException: Invalid name: .FragmentPreferences

dependencies:

compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:preference-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

Please don't ask me to now find a 2.3.5 device to test for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...