在onAttach函数中,eclipse显示错误说明
The method onAttach(Activity) in the type Fragment is not applicable
for the arguments (Context)
尽管很明显传递了Context类型变量
import androID.content.Context;public class MyListFragment extends Fragment{ private OnItemSelectedListener Listener; @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { VIEw vIEw = inflater.inflate(R.layout.fragment_RSSList_overvIEw, container, false); button button = (button) vIEw.findVIEwByID(R.ID.button1); button.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { updateDetail("fake"); } }); return vIEw; } public interface OnItemSelectedListener { public voID onRSSItemSelected(String link); } @OverrIDe public voID onAttach(Context context) { super.onAttach(context); if (context instanceof OnItemSelectedListener) { Listener = (OnItemSelectedListener) context; } else { throw new ClassCastException(context.toString() + " must implemenet MyListFragment.OnItemSelectedListener"); } } @OverrIDe public voID onDetach() { super.onDetach(); Listener = null; } // may also be triggered from the Activity public voID updateDetail(String uri) { // create a string just for testing String newTime = String.valueOf(System.currentTimeMillis()); // inform the Activity about the change based // interface defintion Listener.onRSSItemSelected(newTime); }}
解决方法:
如果您使用的是API<然后23
public voID onAttach(Context context) {
应该
public voID onAttach(Activity context) {
查看official docs
注意:
在API 23中添加了onAttach(Context context).请参见this
总结以上是内存溢出为你收集整理的java-我在onAttach(Context)中遇到了这个奇怪的错误全部内容,希望文章能够帮你解决java-我在onAttach(Context)中遇到了这个奇怪的错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)