gpt4 book ai didi

java - 在 Sony D2203 上运行 PreferenceManager.setDefaultValues 时出现错误,但在 Sony D2403 上运行时它可以正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:09 27 4
gpt4 key购买 nike

索尼 D2203 运行 Android 4.4.2,D2403 运行 Android 4.4.4。

代码中给出错误的具体行是:

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

这是错误的 logcat 输出:

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

这是 MyEditTextPreference 类:

    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);
}
}

首选项 XML 文件:

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

有谁知道为什么我会遇到这个问题?我在网上找不到任何可以帮助的东西。看来我的代码在 Android 4.4.2 和 Android 4.4.4 之间肯定存在一些兼容性问题,但我不知道它是什么。

最佳答案

在我看来,不同的 API 版本在从 XML 扩充时使用 Preference 对象的不同默认构造函数。当子类化膨胀的 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);
}
}

关于java - 在 Sony D2203 上运行 PreferenceManager.setDefaultValues 时出现错误,但在 Sony D2403 上运行时它可以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29796846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com