【问题标题】:Error while creating interface in onAttach method在 onAttach 方法中创建接口时出错
【发布时间】: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


【解决方案1】:

除了来自@Vucko 和@Andreyua 的cmets,我想添加以下内容:

TopSectionListener activityCommander;

而不是

TopSectionFragment activityCommander;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 2022-01-07
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多