2017-10-31 6 views
0

다음 Java 코드를 Kotlin으로 변환하려고합니다. 그것을 컴파일하고 잘 작동합니다. 내가 현재 가지고있는 코드에서 Kotlin 매개 변수 유형이 일치하지 않습니다.

public abstract class MvpViewHolder<P extends BasePresenter> extends RecyclerView.ViewHolder { 
    protected P presenter; 

    public MvpViewHolder(View itemView) { 
     super(itemView); 
    } 

    public void bindPresenter(P presenter) { 
     this.presenter = presenter; 
     presenter.bindView(this); 
    } 

    public void unbindPresenter() { 
     presenter = null; 
    } 
} 

, 나는 Required: Nothing, Found: MvpViewHolder을 내용의 presenter.bindView(this)에 오류가 발생합니다.
abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> { 
    protected var presenter: P? = null 

    fun bindPresenter(presenter: P): Unit { 
     this.presenter = presenter 
     //I get the error here 
     presenter.bindView(this) 
    } 

    fun unbindPresenter(): Unit { 
     presenter = null 
    } 
} 

bindView

그렇게

public abstract class BasePresenter<M,V> { 
    fun bindView(view: V) { 
     this.view = WeakReference(view) 
    } 
} 

처럼 지금 클래스 제네릭을 제대로 정의되지 않은에 속성 수있는 유일한 정의된다. 내가 말할 수있는 한, this은 여전히 ​​매개 변수로 예상되는 View 제네릭의 올바른 인스턴스이며, 역시 Nothing 일 수는 없습니다. 버그를 어떻게 해결할 수 있습니까?

편집 : BasePresenter

public abstract class BasePresenter<M, V> { 
    protected M model; 
    private WeakReference<V> view; 

    public void bindView(@NonNull V view) { 
     this.view = new WeakReference<>(view); 
     if (setupDone()) { 
      updateView(); 
     } 
    } 

    protected V view() { 
     if (view == null) { 
      return null; 
     } else { 
      return view.get(); 
     } 
    } 
} 
+0

보기 변수는 BasePresenter 클래스에서 어떻게 정의됩니까? 코드를 게시 할 수 있습니까? 'protected var view : V? = null'이게 이런가요? –

답변

3

대한 추가 정보를 원하시면 방문을 위해 자바

에서 this을 의미

[email protected]

사용할 수 있습니다 View을 인수로 사용합니다.

null 결과 또는 사용자의 경우 View 객체를 포함 할 수있는 변수 (BasePresenter에 정의 된 view)를 정의하면 표시되는 오류가 나타납니다.

아래 코드에서는 this을 인수로 사용하고 있으며 MapViewHolder는 View의 하위 클래스가 아닙니다.

abstract class MvpViewHolder<P>(itemView: View) : RecyclerView.ViewHolder(itemView) where P : BasePresenter<*,*> { 
    protected var presenter: P? = null 

    fun bindPresenter(presenter: P): Unit { 
     this.presenter = presenter 
     //I get the error here 
     presenter.bindView(this) // -> this references MvpViewHolder which isn't a subclass of View 
    } 

    fun unbindPresenter(): Unit { 
     presenter = null 
    } 
} 

나는 당신이 원하는 것은 그것이 View 객체가 사실 때문에 발표자로 itemView을 첨부라고 생각합니다.

편집

문제는이 경우 BasePresenter<Nothing, Nothing> (아무것도 코 틀린에있는 개체 없음) 의미 BasePresenter<*,*>을 정의과 관련이있다 - this 링크를 코 틀린 더 스타 예측 에 대한를 참조하십시오.

기본 표현자가 예상하거나 명시 적으로 BasePresenter<Any?, Any?>으로 정의하는 유형을 명시 적으로 정의하는 것이 좋습니다.

+0

약간 보았지만 자바 측에서는 문제가되지 않는다는 것을 깨달았습니다. 그렇다면 Java 코드는 어떻게 작동합니까? – Rafa

+0

'BasePresenter'에 자바 코드를 보여줄 수 있습니까? –

+0

나는 편집을했습니다 – Rafa