启动首选项屏幕时,Android应用程序立即崩溃

时间:2011-04-23 16:28:35

标签: java android xml sharedpreferences

我一直在为应用程序设置首选项屏幕,我甚至没有让它在崩溃之前显示屏幕。我已经检查了几个关于共享首选项的教程,我的代码类似于他们的代码,但没有任何工作。我认为问题出在我的preferences.xml文件中,因为除了在PreferenceActivity中打开XML文件之外,我注释掉了所有内容。

这是我的preferences.xml文件:

<?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>

而且,如果我错了,我的问题是我的java文件,这里是PreferenceActivity。目前大多数都被注释掉了,但程序仍然崩溃。

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();*/

    }   
}

以下是崩溃的LogCat信息:

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

5 个答案:

答案 0 :(得分:3)

也许您更改了首选架构,尝试删除存储在/ dbdata / databases / com_your_app / shared_prefs /或/ data / data / com_your_app中的旧优先文件。

答案 1 :(得分:3)

在你创建的偏好活动中你可能有这个:

setContentView(R.xml.prefs);

你需要这样的东西:

addPreferencesFromResource(R.xml.prefs);

请参阅:PreferenceScreen class not found

答案 2 :(得分:1)

不知何故,该应用认为PreferenceScreen属于包android.view,但系统文档仅在包android.preference

中知道它
 Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen in loader dalvik.system.PathClassLoader[/data/app/com.cs.fsu.edu.project4-1.apk]

您是否以某种方式在应用中添加了一些名为PreferenceScreen的类,这可能会使系统混乱?

答案 3 :(得分:1)

我想在这里加分。我的经验是,总是做一个干净的构建,总是检查你的清单的意图,活动,过滤器,权限。

检查软件包名称,有时您重命名可能出错的软件包。

在安装之前删除以前的应用程序(在不同的PC上创建?),删除旧设置。

这一切都与我有关: - )

如果这不起作用,请将所有文件复制到新项目。愚蠢,但这对我有用 - 取消评论一切,仍然没有奏效。然后:ping,start&amp;去。

答案 4 :(得分:1)

确保您拥有AndroidManifest.xml文件中列出的活动

<activity android:name=".Preferences"></activity>