0
我有这样的BaseFragment:通用BaseFragment验证实现的接口回调
public abstract class BaseFragment<C> extends Fragment {
protected C callback;
@Override
@SuppressWarnings({"unchecked"})
public void onAttach(final Context context) {
super.onAttach(context);
try {
this.callback = (C) getActivity();
} catch (final ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement " + this.callback.getClass().getSimpleName());
}
}
}
我有一个执行片段:
public class MainFragment<C extends MainFragment.Callback> extends BaseFragment<C> {
public static MainFragment newInstance() {
MainFragment<Callback> fragment = new MainFragment<>();
return fragment;
}
public interface Callback {
void doSomething();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
return view;
}
}
我创建一个静态的newInstance方法的片段。 当我在onAttach中放置断点时,它永远不会失败,我的活动甚至没有实现回调。
我做错了什么?