2017-05-27 117 views
0

我正在将项目转换为Kotlin。当我转换对话框首选项时,应用程序崩溃了“没有这样的方法”,显然构造函数丢失。这是错误消息:DialogPreference缺少构造函数

05-26 20:13:32.799 6019 6019 E AndroidRuntime: Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 
05-26 20:13:32.799 6019 6019 E AndroidRuntime: at java.lang.Class.getConstructor0(Class.java:2204) 
05-26 20:13:32.799 6019 6019 E AndroidRuntime: at java.lang.Class.getConstructor(Class.java:1683) 
05-26 20:13:32.799 6019 6019 E AndroidRuntime: at android.preference.GenericInflater.createItem(GenericInflater.java:378) 

类有2个参数的构造函数的错误消息抱怨:

class FilterPreferenceFragment(context: Context, attrs: AttributeSet?) : 
    BaseDialogPreference(context, attrs) { 
    /* */ 
} 

和基础类:

abstract class BaseDialogPreference(context: Context, attrs: AttributeSet?) : 
    DialogPreference(context, attrs) { 
    /* */ 
} 

有什么建议?

编辑:

的情况下,它是有帮助的,这里是由科特林编译器生成的字节码:

// access flags 0x1 
    public <init>(Landroid/content/Context;Landroid/util/AttributeSet;)V 
    @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 
    @Lorg/jetbrains/annotations/Nullable;() // invisible, parameter 1 
    L0 
    ALOAD 1 
    LDC "context" 
    INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V 
    L1 
    LINENUMBER 12 L1 
    ALOAD 0 
    ALOAD 1 
    ALOAD 2 
    L2 
    LINENUMBER 13 L2 
    INVOKESPECIAL android/preference/DialogPreference.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V 
    RETURN 
    L3 
    LOCALVARIABLE this Lcom/example/exampleapp/app/preference/BaseDialogPreference; L0 L3 0 
    LOCALVARIABLE context Landroid/content/Context; L0 L3 1 
    LOCALVARIABLE attrs Landroid/util/AttributeSet; L0 L3 2 
    MAXSTACK = 3 
    MAXLOCALS = 3 

    @Lkotlin/Metadata;(mv={1, 1, 6}, bv={1, 0, 1}, k=1, d1={"\u0000$\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0008\u0002\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0008&\u0018\u00002\u00020\u0001B\u0017\u0012\u0006\u0010\u0002\u001a\u00020\u0003\u0012\u0008\u0010\u0004\u001a\u0004\u0018\u00010\u0005\u00a2\u0006\u0002\u0010\u0006J\u0010\u0010\u0007\u001a\u00020\u00082\u0006\u0010\u0009\u001a\u00020\nH\u0014\u00a8\u0006\u000b"}, d2={"Lcom/example/exampleapp/app/preference/BaseDialogPreference;", "Landroid/preference/DialogPreference;", "context", "Landroid/content/Context;", "attrs", "Landroid/util/AttributeSet;", "(Landroid/content/Context;Landroid/util/AttributeSet;)V", "onBindView", "", "view", "Landroid/view/View;", "production sources for module ExampleApp"}) 
    // compiled from: BaseDialogPreference.kt 
+0

您需要在kotlin中使用次级构造函数DialogPreference类包含多个构造函数。有关二级构造函数的更多信息。 https://kotlinlang.org/docs/reference/classes.html – mcd

+0

这个二级构造函数的签名应该是什么? – Francesc

+0

啊对不起。我尝试它,你忘了添加它的运行?在上下文中。上下文也是无效的,所以添加?上下文之后?和它的作品。 例如 open class B(context:Context ?, attrs:AttributeSet?):DialogPreference(context,attrs) – mcd

回答

0

原来我一直在寻找在错误的类。

class BaseCheckBoxPreference(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : CheckBoxPreference(context, attrs, defStyleAttr) { /* */ } 

为了解决死机,我不得不删除从构造函数中的第三个参数:从这个其他偏好附带的错误或者

class BaseCheckBoxPreference(context: Context, attrs: AttributeSet? = null) : CheckBoxPreference(context, attrs) { /* */ } 

,我们可以定义一个次级构造函数,只需要2个参数,但对于我的用例,我只需要带有2个参数的那个。次要构造函数将是

class BaseCheckBoxPreference(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : CheckBoxPreference(context, attrs, defStyleAttr) { 

constructor(context: Context, attrs: AttributeSet? = null) : this(context, attrs, 0) 

/* */ 
} 
相关问题