我试图从一本书中重复一个例子,其源代码已经在同一部手机上进行了测试,但是我没有。我比较了源代码,它们完全相同。
错误是:
04-05 15:30:33.004: E/AndroidRuntime(20354): FATAL EXCEPTION: main
04-05 15:30:33.004: E/AndroidRuntime(20354): Process: org.crazyit.app, PID: 20354
04-05 15:30:33.004: E/AndroidRuntime(20354): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.crazyit.app/org.crazyit.app.PreferenceActivityTest}: android.view.InflateException: Binary XML file line #5: Error inflating class RingtongPreference
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2200)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread.access$800(ActivityThread.java:139)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.os.Handler.dispatchMessage(Handler.java:102)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.os.Looper.loop(Looper.java:136)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread.main(ActivityThread.java:5105)
04-05 15:30:33.004: E/AndroidRuntime(20354): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 15:30:33.004: E/AndroidRuntime(20354): at java.lang.reflect.Method.invoke(Method.java:515)
04-05 15:30:33.004: E/AndroidRuntime(20354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-05 15:30:33.004: E/AndroidRuntime(20354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
04-05 15:30:33.004: E/AndroidRuntime(20354): at dalvik.system.NativeStart.main(Native Method)
04-05 15:30:33.004: E/AndroidRuntime(20354): Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class RingtongPreference
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
04-05 15:30:33.004: E/AndroidRuntime(20354): at org.crazyit.app.PreferenceActivityTest$Prefs1Fragment.onCreate(PreferenceActivityTest.java:54)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.Fragment.performCreate(Fragment.java:1688)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:860)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.BackStackRecord.run(BackStackRecord.java:684)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1450)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.Activity.performStart(Activity.java:5284)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
04-05 15:30:33.004: E/AndroidRuntime(20354): ... 11 more
04-05 15:30:33.004: E/AndroidRuntime(20354): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.RingtongPreference" on path: DexPathList[[zip file "/data/app/org.crazyit.app-1.apk"],nativeLibraryDirectories=[/data/app-lib/org.crazyit.app-1, /vendor/lib, /system/lib]]
04-05 15:30:33.004: E/AndroidRuntime(20354): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
04-05 15:30:33.004: E/AndroidRuntime(20354): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
04-05 15:30:33.004: E/AndroidRuntime(20354): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.createItem(GenericInflater.java:375)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
04-05 15:30:33.004: E/AndroidRuntime(20354): at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
04-05 15:30:33.004: E/AndroidRuntime(20354): ... 24 more
包含RingtonePrefrences的XML文件是:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<RingtongPreference
android:ringtoneType="all"
android:title="Ringtone Settings"
android:summary="Choose Ringtones(Test RingtonePrerference)"
android:showDefault="true"
android:key="ring_key"
android:showSilent="true">
</RingtongPreference>
<PreferenceCategory android:title="Personal Info">
<EditTextPreference
android:key="name"
android:title="Please fill in your username"
android:summary="Please fill in your username(Test EditTextPreference)"
android:dialogTitle="Your username:"/>
<ListPreference
android:key="gender"
android:title="Gender"
android:summary="Gender(Test ListPreference)"
android:dialogTitle="ListPreference"
android:entries="@array/gender_name_list"
android:entryValues="@array/gender_value_list"/>
</PreferenceCategory>
<PreferenceCategory android:title="System Settings">
<CheckBoxPreference
android:key="autoSave"
android:title="AutoSave"
android:summaryOn="AutoSave: On"
android:summaryOff="AutoSave: Off"
android:defaultValue="true"/>
</PreferenceCategory>
</PreferenceScreen>
用于膨胀xml的活动是:
package org.crazyit.app;
import java.util.List;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.widget.Button;
import android.widget.Toast;
public class PreferenceActivityTest extends PreferenceActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(hasHeaders())
{
Button button = new Button(this);
button.setText("Settings");
setListFooter(button);
}
}
@Override
public void onBuildHeaders(List<Header> target)
{
loadHeadersFromResource(R.xml.preference_headers, target);
}
public static class Prefs1Fragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
public static class Prefs2Fragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.display_prefs);
String website = getArguments().getString("website");
Toast.makeText(getActivity(),
website, Toast.LENGTH_LONG).show();
}
}
}
以防万一,我将在这里包含AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="org.crazyit.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".OtherActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ExpandableListActivityTest"
android:label="See Categories">
</activity>
<activity
android:name=".PreferenceActivityTest"
android:label="Settings">
</activity>
</application>
</manifest>
我试图在整个下午寻找这个错误而没有找到它。请帮忙。感谢。
答案 0 :(得分:1)
Android没有RingtongPreference
,这是您的首选XML所要求的。我的猜测是,这是一个拼写错误,你的意思是RingtonePreference
,就像你的问题标题一样。