在我使用支持库v7的应用程序中,我想添加首选项屏幕。由于Google完全没有提供有关该主题的文档,因此我在Stack Overflow上查找了其他地方的帖子。
所以这是我的项目:
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>
如果我尝试启动此首选项活动,我会收到异常。
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)
这段代码有什么问题?这应该工作。给出的例外是令人费解的,并没有提供任何线索。 Google没有提供任何文档,也没有工作示例。我觉得可疑的是这条线:
引起:java.lang.ClassNotFoundException:android.view.fragment in 装载机 dalvik.system.PathClassLoader [/data/app/com.example.testandroidsupportv7-2.apk]
如何摆脱这种异常?
更新
我在Android 2.3.5设备上运行。 我添加了preference.xml
答案 0 :(得分:7)
您的logcat提示错误:XML file line #11: Error inflating class fragment
- 您可能想要name=".FragmentPreferences"
的可疑行似乎是android:name=".FragmentPreferences"
。既然你已经拥有它,只需删除错误的行:)
<fragment
android:name=".FragmentPreferences"
android:tag=".FragmentPreferences"
android:layout_width="match_parent"
android:layout_height="match_parent" />
更新:如果不起作用,那么另一个明显的错误就是android:name
值。它应该是:
android:name="com.example.testandroidsupportv7.FragmentPreferences"
或任何
我甚至建立了你的项目,花了不到5分钟的时间来创建,测试和确认错误;我得到了与你完全相同的错误,当我修复android:name
它构建得很好。截图:
我希望你会因为你的行为而向所有人道歉......不是如何在SO上获得人们的帮助。
UPDATE 2:来自我的构建的部分logcat,错误的android:name值:
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设备来测试你。