如何使用preferences在Android的支持库V7 rev23? [英] How to use preferences in Android support library v7 rev23?

查看:280
本文介绍了如何使用preferences在Android的支持库V7 rev23?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用支持库V7我的应用我想补充一个preference屏幕。由于谷歌提供了在这个问题上绝对没有文件我已经看过了其他地方发现这里对堆栈溢出后。

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>

活动preferences.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);
    }

}

片段preferences.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>

如果我尝试启动这个preference活动我得到一个例外。

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)

什么是不对的code?这应该工作。给出的例外是令人费解,并没有提供线索。谷歌没有提供任何文件,也没有工作的例子。我觉得可疑的是该行:

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:

致:抛出java.lang.ClassNotFoundException:android.view.fragment在   装载机   dalvik.system.PathClassLoader [/data/app/com.example.testandroidsupportv7-2.apk]

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

如何摆脱这种异常?

更新

我在Android 2.3.5设备上运行。 我加入了preference.xml

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

推荐答案

您的logcat暗示了错误: XML文件中的行#11:错误充气类片段 - 中可疑的行似乎有 NAME =片段preferences。,你可能想成为机器人:名称=片段preferences。既然你已经有一个,只是删除了错误路线:)

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"

或一切的实际路径片段preferences类。

or whatever is YOUR actual path to the FragmentPreferences class.

我甚至建项目对我的目的,采取了不到5分钟全部创建,测试,并确认错误;我得到了完全相同的错误,因为你,当我固定安卓名称它建立就好了。截图:

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:

输入图像的描述在这里

我希望你能道歉,大家对你的行为......这不是如何去从人得到帮助的左右。

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

更新2:部分从我的生成的logcat,与错误的机器人:名称值:

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

相关性:

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'

请不要问我现在发现一个2.3.5设备来测试你。

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

这篇关于如何使用preferences在Android的支持库V7 rev23?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆