Android App crashes immediately when preferences screen is started
I have been working on a preferences screen for an app, and I haven't even gotten it to display the screen before it crashes. I have examined several tutorials about shared preferences and my code resembles theirs, but nothing has worked. I think the problem is in my preferences.xml file, because I commented out everything except the opening of the XML file in my PreferenceActivity.
Here is my preferences.xml file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Password">
<EditTextPreference
android:name="Your Password"
android:title="Password"
android:defaultValue=""
android:summary="For logging in if facial recognition fails"
android:key="password" />
<EditTextPreference
android:name="Max Attempts"
android:title="Max Attempts"
android:defaultValue="3"
android:summary="Max number of times to try authentication before falling back to password"
android:key="maxAttempts" />
</PreferenceCategory>
<PreferenceCategory
android:title="Security">
<CheckBoxPreference
android:title="Intruder Alert"
android:defaultValue="false"
android:summary="Notify me if you detect a face other than mine"
android:key="intruderAlert" />
</PreferenceCategory>
<PreferenceCategory
android:title="Intruder Notification">
<EditTextPreference
android:name="Your Email"
android:title="email"
android:defaultValue=""
android:summary="An email address for us to notify"
android:key="email" />
<EditTextPreference
android:name="Your Phone Number"
android:title="phoneNumber"
android:defaultValue=""
android:summary="A phone number for us to notify"
android:key="phoneNumber" />
</PreferenceCategory>
</PreferenceScreen>
And, in case I'm wrong and my problem is my java file, here's the PreferenceActivity. Most is commented out at the moment, but the program still crashes.
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener {
/*private EditTextPreference password;
private EditTextPreference maxAttempts;
private CheckBoxPreference intruderAlert;
private EditTextPreference email;
private EditTextPreference phoneNumber;
String PassWord;
int MaxAttempts;
boolean IntruderAlert;
String Email;
String PhoneNumber;*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
//password = (EditTextPreference) getPreferenceScreen().findPreference("password");
//maxAttempts = (EditTextPreference) getPreferenceScreen().findPreference("maxAttempts");
//intruderAlert = (CheckBoxPreference) getPreferenceScreen().findPreference("intruderAlert");
//email = (EditTextPreference) getPreferenceScreen().findPreference("email");
//phoneNumber = (EditTextPreference) getPreferenceScreen().findPreference("phoneNumber");
}
private void setSummaries()
{
}
@Override
protected void onResume开发者_运维技巧()
{
/*super.onResume();
String intruderAlertSetting;
if (intruderAlert.isChecked())
intruderAlertSetting = "on";
else
intruderAlertSetting = "off";
password.setSummary("Your password is " +password.getText()+ ".");
maxAttempts.setSummary("The max attempts that will be made is "+maxAttempts.getText() + ".");
intruderAlert.setSummary("You have intruder alert set to" + intruderAlertSetting+".");
if (email.getText().equals(""))
email.setSummary("You have no email address stored.");
else
email.setSummary("Your stored email address is "+email.getText() + ".");
if (phoneNumber.getText().equals(""))
phoneNumber.setSummary("You have no phone number stored.");
else
phoneNumber.setSummary("Your stored phone number is " + phoneNumber.getText()+".");
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);*/
}
@Override
protected void onPause()
{
/*super.onPause();
String intruderAlertSetting;
if (intruderAlert.isChecked())
intruderAlertSetting = "on";
else
intruderAlertSetting = "off";
password.setSummary("Your password is " +password.getText()+ ".");
maxAttempts.setSummary("The max attempts that will be made is "+maxAttempts.getText() + ".");
intruderAlert.setSummary("You have intruder alert set to" + intruderAlertSetting+".");
if (email.getText().equals(""))
email.setSummary("You have no email address stored.");
else
email.setSummary("Your stored email address is "+email.getText() + ".");
if (phoneNumber.getText().equals(""))
phoneNumber.setSummary("You have no phone number stored.");
else
phoneNumber.setSummary("Your stored phone number is " + phoneNumber.getText()+".");
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);*/
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key)
{
/*String intruderAlertSetting;
if (intruderAlert.isChecked())
intruderAlertSetting = "on";
else
intruderAlertSetting = "off";
password.setSummary("Your password is " +password.getText()+ ".");
maxAttempts.setSummary("The max attempts that will be made is "+maxAttempts.getText() + ".");
intruderAlert.setSummary("You have intruder alert set to" + intruderAlertSetting+".");
if (email.getText().equals(""))
email.setSummary("You have no email address stored.");
else
email.setSummary("Your stored email address is "+email.getText() + ".");
if (phoneNumber.getText().equals(""))
phoneNumber.setSummary("You have no phone number stored.");
else
phoneNumber.setSummary("Your stored phone number is " + phoneNumber.getText()+".");
PassWord = password.getText();
MaxAttempts = Integer.parseInt(maxAttempts.getText());
IntruderAlert = intruderAlert.isChecked();
Email = email.getText();
PhoneNumber = phoneNumber.getText();*/
}
}
Here is the LogCat info on the crash:
04-23 16:37:09.181: WARN/dalvikvm(818): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): FATAL EXCEPTION: main
04-23 16:37:09.221: ERROR/AndroidRuntime(818): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cs.fsu.edu.project4/com.cs.fsu.edu.project4.Preferences}: android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.os.Looper.loop(Looper.java:123)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread.main(ActivityThread.java:3647)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.Activity.setContentView(Activity.java:1657)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at com.cs.fsu.edu.project4.Preferences.onCreate(Preferences.java:33)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): ... 11 more
04-23 16:37:09.221: ERROR/AndroidRuntime(818): Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen in loader dalvik.system.PathClassLoader[/data/app/com.cs.fsu.edu.project4-1.apk]
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-23 16:37:09.221: ERROR/AndroidRuntime(818): ... 19 more
04-23 16:37:09.251: WARN/ActivityManager(62): Force finishing activity com.cs.fsu.edu.project4/.Preferences
Maybe you change your preference schema, try by removing old preferencies files stored in /dbdata/databases/com_your_app/shared_prefs/ or /data/data/com_your_app.
In your Preference Activity onCreate you probably had this:
setContentView(R.xml.prefs);
you need something like this instead:
addPreferencesFromResource(R.xml.prefs);
Refer to: PreferenceScreen class not found
Somehow the app thinks PreferenceScreen
is of package android.view
, but the system docs only knows it in package android.preference
Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen in loader dalvik.system.PathClassLoader[/data/app/com.cs.fsu.edu.project4-1.apk]
Did you somehow add some class named PreferenceScreen in your app, which may confuse the system?
I'd like to add my cents here. My experience is, always make a clean build, always check your manifest for intents, activities, filters, permissions.
Check package names, sometimes you renamed packages which can go wrong.
Remove the previous app (created on differnt PC?) before installing, remove the old settings.
All this went wrong with me :-)
If this doesn't work, copy all files to a new project. Stupid, but this worked for me - uncommented everything, still didn't work. Then: ping, start & go.
Make sure you have the activity listed in the AndroidManifest.xml file
<activity android:name=".Preferences"></activity>
精彩评论