2016-07-27 12 views
0

onAttach方法中出现“Incompatible types”错误。我试图在这里创建接口,但不知道为什么会发生这种情况,我认为我做到了正确的方式。而且当我调用“createMeme”方法时,它给了我错误,“不解析这种方法”,而这种方法我公开。理解我的问题比plz问的任何混淆,我是一个可怕的提问者任何方式。任何人有任何想法?感谢提前:)在onAttach方法中创建接口时出错

public class TopSectionFragment extends Fragments 
{ 
private static EditText toptextInput; 
private static EditText bottomtextInput; 

TopSectionFragment activityCommander; 

public interface TopSectionListener 
{ 
    public void createMeme(String topText, String bottomText); 
} 

@Override 
@SuppressWarnings("deprecation") 
public void onAttach(Activity activity) 
{ 
    super.onAttach(activity); 
    try { 
     activityCommander = (TopSectionListener) activity;//error here 
    }catch(ClassCastException e) 
    { 
     throw new ClassCastException (activity.toString()); 
    } 
} 
+1

首先, ,'onAttach'这个声明被弃用,它应该以'Context context'作为参数。其次,展示你的活动。它是否实现了'TopSetionListener' – Vucko

+1

也许你没有实现TopSectionListener到Activity? – Andreyua

+0

@Andreyua我实现了它。但它仍然给出同样的错误。因此我改变了activityCommander的类型并删除了错误。 –

回答

0

除了从@Vucko和@Andreyua的意见,我想补充以下内容:

TopSectionListener activityCommander; 

代替

TopSectionFragment activityCommander; 
+0

是的,我改变了它,错误消失了。 :) –