【发布时间】:2016-07-27 06:59:43
【问题描述】:
我在 onAttach 方法中收到错误“不兼容的类型”。我试图在这里创建界面,但不知道为什么会这样,我认为我做对了。而且当我调用“createMeme”方法时,它给了我错误“con not resolve this method”,而这个方法是公开的。在理解我的问题时有任何困惑,而不是问,无论如何我都是一个可怕的提问者。有人知道吗?在此先感谢:)
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());
}
}
【问题讨论】:
-
首先,这个
onAttach的声明已经被废弃了,它应该以Context context作为参数。其次,显示您的活动。是否实现TopSetionListener -
@Andreyua 我实现了它。但它仍然给出了同样的错误。因此我更改了 activityCommander 的类型并删除了错误。
标签: java android android-studio android-fragments interface