我目前正在尝试将设置活动添加到我的Android应用程序中。我根据Android的官方教程的推荐,选择了一个扩展PreferenceFragment片段的SettingsActivity。
我正在尝试在应用顶部设置工具栏,再次遵循Android的教程。
本教程建议您在主要活动中设置默认值,这就是我所做的。
我在加载应用程序时遇到错误。我已经注释掉导致错误的行,如果我这样做,应用程序加载就好了,所以我把它缩小到这一行。
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
preferences.xml确实存在于/ res / xml /.
中我还阅读了有关Android设置的教程,但它们似乎对我自己没有任何不同。我也看过其他人在这里发布的问题,但他们收到的错误似乎是因为他们包括了图形而我的不是。
这是我的preferences.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!--Checkbox for series plot options grouped in subscreen-->
<PreferenceScreen
android:key="series_option_key"
android:title="@string/checkbox_option_title"
android:persistent="false">
<ListPreference
android:key="list_option_key"
android:title="@string/checkbox_option_title"
android:entries="@array/series_options"
android:defaultValue="@string/series_default">
</ListPreference>
</PreferenceScreen>
</PreferenceScreen>
</resources>
包含设置页面的片段:
public class Settings_ActivityFragment extends PreferenceFragment {
public Settings_ActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Load preferences from xml file
addPreferencesFromResource(R.xml.preferences);
return inflater.inflate(R.layout.fragment_settings_, container, false);
}
}
此片段所在的活动:
public class Settings_Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new Settings_ActivityFragment())
.commit();
}
}
与设置片段相关联的关联xml布局文件除了指定相对布局外,其中没有任何内容,因为我没有找到需要向此文件添加内容的任何地方。
你所有人都在等待......堆栈跟踪!
08-09 14:31:05.989 2470-2470/cmackie.nationalprofitgraphgenerator E/AndroidRuntime: FATAL EXCEPTION: main
Process: cmackie.nationalprofitgraphgenerator, PID: 2470
java.lang.RuntimeException: Unable to start activity ComponentInfo{cmackie.nationalprofitgraphgenerator/cmackie.nationalprofitgraphgenerator.Option_Activity}: android.view.InflateException: Binary XML file line #2: Error inflating class resources
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
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.PreferenceManager.setDefaultValues(PreferenceManager.java:486)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:445)
at cmackie.nationalprofitgraphgenerator.Option_Activity.onCreate(Option_Activity.java:37)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[zip file "/data/app/cmackie.nationalprofitgraphgenerator-2/base.apk"],nativeLibraryDirectories=[/data/app/cmackie.nationalprofitgraphgenerator-2/lib/arm, /vendor/lib, /system/lib]]
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.PreferenceManager.setDefaultValues(PreferenceManager.java:486)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:445)
at cmackie.nationalprofitgraphgenerator.Option_Activity.onCreate(Option_Activity.java:37)
at android.app.Activity.performCreate(Activity.java:6245)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Suppressed: java.lang.ClassNotFoundException: Didn't find class "android.preference.resources" on path: DexPathList[[dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-support-annotations-24.1.1_260a6af0dfc8036856ad6ba2b9d281e7118bf977-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/cmackie.nationalprofitgraphgenerator/files/in
我很乐意根据需要发布任何其他代码。
答案 0 :(得分:1)
根据MAX_VERTEX_ATTRIBS
的官方文档页面上的示例,您需要从您的preferences.xml屏幕布局中删除PreferenceScreen
标记。因此,您的布局应如下所示:
<resources>