如何修复“未选中调用'attachView(V)'作为原始类型'BasePresenter'”的成员?

时间:2017-01-05 12:23:53

标签: java android generics mvp dagger-2

我已经在这里找到了很多关于这个问题的答案,但对于我的生活,我似乎无法解决这个问题,我需要一些帮助。

BasePresenter:

public abstract class BasePresenter<V> {

    private V mView;

    public void attachView(V view) { mView = view; }
    public void detachView() { mView = null; }

}

BaseFragment:

public abstract class BaseFragment<P extends BasePresenter> extends Fragment {

    @Inject protected P mPresenter;

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.attachView(this); // unchecked call to 'attachView(V)' as a member of raw type 'BasePresenter'
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.detachView();
    }

}

MyPresenter:

public class MyPresenter extends BasePresenter<MyPresenter.MyView> {

    @Inject
    public MyPresenter() {}

    public interface MyView {}

}

MyFragment:

public class MyFragment extends BaseFragment implements MyPresenter.MyView {}

2 个答案:

答案 0 :(得分:3)

问题在于类型变量声明:

class BaseFragment<P extends BasePresenter>

BasePresenter是一个泛型类,因此您需要指定其类型参数。从这个片段:

mPresenter.attachView(this);

看起来你希望BaseFragment绑定到BasePresenter中的类型变量V - 所以我会按如下方式重写你的BaseFragment声明:

abstract class BaseFragment<P extends BasePresenter<BaseFragment<P>>> { ... }

这应该照顾未经检查的警告。也就是说,我强烈怀疑你真正追求的是这样的:

abstract class BaseFragment<P extends BasePresenter<V>, V> { ... }

其中V是一个独立的类型变量,用于建模'视图'。

答案 1 :(得分:1)

简单,而不是

public abstract class BaseFragment<P extends BasePresenter> extends Fragment {

应该是

public abstract class BaseFragment<V, P extends BasePresenter<V>> extends Fragment { 

public abstract class BaseFragment<P extends BasePresenter<BaseFragment<P>>> extends Fragment { 

public abstract class BaseFragment<V extends BaseFragment<V, P>, P extends BasePresenter<V>> extends Fragment { 

基本上,请确保BasePresenter参与其中。

编辑:

好的,根据你实际上要做的事情,你应该这样做:

public abstract class BasePresenter<V> {

    private V mView;

    public void attachView(V view) { mView = view; }
    public void detachView() { mView = null; }
}

public abstract class BaseFragment<V extends BaseFragment<V, P>, P extends BasePresenter<V>> extends Fragment {

    protected abstract P getPresenter();

    @Override
    public void onResume() {
        super.onResume();
        getPresenter().attachView(this); // unchecked call to 'attachView(V)' as a member of raw type 'BasePresenter'
    }

    @Override
    public void onPause() {
        super.onPause();
        getPresenter().detachView();
    }
}

public class MyPresenter extends BasePresenter<MyPresenter.MyView> {
    @Inject
    public MyPresenter() {}

    public interface MyView {}
}

public class MyFragment extends BaseFragment<MyFragment, MyPresenter> implements MyPresenter.MyView {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyFragmentComponent component = ((MainActivity)getActivity()).getComponent().myFragmentComponent();
        component.inject(this);
        View view = inflater.inflate(R.layout.blah, container, false);
        ButterKnife.bind(this, view);
        return view;
    }
}

EDIT2:基于提供的示例:

public class RexTester {
    // MAIN CLASS

    static class Rextester {
        public static void main(String args[]) {
            new MyFragment();
        }
    }

// MVP CODE

    interface BaseView {}

    final static class MyPresenter extends BasePresenter<MyPresenter.MyView> {

        public MyPresenter() {}

        public void executeAction() {
            mView.onCallback();
        }

        interface MyView extends BaseView {
            void onCallback();
        }

    }

    abstract static class BasePresenter<V extends BaseView> {

        protected V mView;

        public void attachView(V view) { mView = view;}
        public void detachView() { mView = null; }

    }

    final static class MyFragment extends BaseFragment<MyPresenter.MyView, MyPresenter> implements MyPresenter.MyView {

        private MyPresenter mPresenter;

        public MyFragment() {
            mPresenter = new MyPresenter();

            onResume(); // Mock onResume() lifecycle event!

            mPresenter.executeAction();

            onPause(); // Mock onPause() lifecycle event!
        }

        protected MyPresenter getPresenter() {
            return mPresenter;
        }

        @Override
        protected MyPresenter.MyView getThis() {
            return this;
        }

        public void onCallback() {
            System.out.println("Hello AndroidMVP!");
        }
    }

    abstract static class BaseFragment<V extends BaseView, P extends BasePresenter<V>> extends Fragment implements BaseView {

        protected abstract P getPresenter();

        protected void onResume() {
            super.onResume();
            getPresenter().attachView(getThis());
        }

        protected abstract V getThis();

        protected void onPause() {
            super.onPause();
            getPresenter().detachView();
        }
    }

// ANDROID FRAMEWORK MOCK

    abstract static class Fragment {

        protected void onResume() {}

        protected void onPause() {}

    }
}