1

The Sony D2203 runs Android 4.4.2 and the D2403 runs Android 4.4.4.

The specific line in the code that is giving the error is:

PreferenceManager.setDefaultValues(this,R.xml.preference,false);

This Is the logcat output of the error:

4-22 13:13:42.542  24913-24913/petra.com.wecapture E/AndroidRuntime? FATAL EXCEPTION: main
    Process: petra.com.wecapture, PID: 24913
    java.lang.RuntimeException: Unable to start activity ComponentInfo{petra.com.wecapture/petra.com.wecapture.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class petra.com.settings.MyEditTextPreference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5136)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class petra.com.settings.MyEditTextPreference
            at android.preference.GenericInflater.createItem(GenericInflater.java:386)
            at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
            at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
            at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
            at android.preference.GenericInflater.inflate(GenericInflater.java:326)
            at android.preference.GenericInflater.inflate(GenericInflater.java:263)
            at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
            at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485)
            at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
            at petra.com.wecapture.MainActivity.onCreate(MainActivity.java:221)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5136)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
            at java.lang.Class.getConstructorOrMethod(Class.java:472)
            at java.lang.Class.getConstructor(Class.java:446)
            at android.preference.GenericInflater.createItem(GenericInflater.java:377)
            at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
            at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
            at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
            at android.preference.GenericInflater.inflate(GenericInflater.java:326)
            at android.preference.GenericInflater.inflate(GenericInflater.java:263)
            at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
            at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:485)
            at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
            at petra.com.wecapture.MainActivity.onCreate(MainActivity.java:221)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5136)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
            at dalvik.system.NativeStart.main(Native Method)
device not found

Here is the MyEditTextPreference class:

    import android.content.Context;
    import android.preference.EditTextPreference;
    import android.util.AttributeSet;


    public class MyEditTextPreference extends EditTextPreference {
        public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        @Override
        public void setText(String text) {
            super.setText(text);
            setSummary(text);
        }
    }

The preference XML file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="App settings"
        android:key="pref_key_storage_settings">
    <petra.com.settings.MyEditTextPreference
        android:key="pref_serverpath"
        android:title="Server address"
        android:defaultValue="http://192.168.0.1/"
     />



        <petra.com.settings.MyEditTextPreference
            android:key="pref_servicename"
            android:title="Service Name"
            android:defaultValue="Service1.svc"
            />
        <CheckBoxPreference
            android:key="pref_firstrun"
            android:title="firstrun"
            android:defaultValue="true"
             />
</PreferenceCategory>
<PreferenceCategory
    android:title="Local Database Tools"
    android:key="pref_key_db_settings">
    <Preference
        android:key="pref_clean_db"
        android:title="Clean database"
        android:summary="All data in local database will be lost."
        android:enabled="true">
    </Preference>

</PreferenceCategory>


</PreferenceScreen>

Does anyone know why I would get this problem? I can't find anything to help on the web. It seems like there must be some compatibility issue with my code between Android 4.4.2 and Android 4.4.4 but I don't know what it is.

4

1 回答 1

0

在我看来,不同的 API 版本Preference在从 XML 膨胀时使用对象的不同默认构造函数。您应该始终实现所有默认构造函数,并将子类化为膨胀的 android 对象。你MyEditTextPreference应该看起来像这样

public class MyEditTextPreference extends EditTextPreference {

    public MyEditTextPreference(Context context) {
        super(context);
    }

    public MyEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setText(String text) {
        super.setText(text);
        setSummary(text);
    }
}
于 2015-04-22T12:33:34.560 回答