简体   繁体   English

覆盖 android.support.v4.app.fragment 中已弃用的方法

[英]Override deprecated method in android.support.v4.app.fragment

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        activitycomander = (TopSectionListener) activity;
    }catch (ClassCastException e){
        throw new ClassCastException(activity.toString());
    }
}

This is the code of my java class linking with MainActivity.这是我的 java 类与 MainActivity 链接的代码。 There is issue with onAttach method in Android Studio. Android Studio 中的onAttach方法存在问题。 It shows as strike-through with the message:它显示为带有删除线的消息:

onAttach ( android.app.activity ) is deprecated: onAttach ( android.app.activity ) 已弃用:

I'm also having a second issue in MainActivity:我在 MainActivity 中还有第二个问题:

 public void createMeme(String top, String bottom){
    bottomsection fragmentbottom = (bottomsection) getSupportFragmentManager().findFragmentById(R.id.fragment2);
    fragmentbottom.setMemeText(top, bottom);
}

topsection and bottomsection are the names of my java classes, they are highlighted red with the message cannot resolve method . topsectionbottomsection是我的 Java 类的名称,它们以红色突出显示,并带有消息cannot resolve method

I have done a clean rebuild on my project and checked the option to add or remove the imports automatically.我已经对我的项目进行了干净的重建,并检查了自动添加或删除导入的选项。

First question regarding onAttach is a duplicate of Android Fragment onAttach() deprecated关于onAttach第一个问题是Android Fragment onAttach() deprecated的副本

Regarding topsection and bottomsection , Classes should always begin with a capital letter and use CamelCaps, as per the Naming Conventions .关于topsectionbottomsection ,根据命名约定,类应始终以大写字母开头并使用 CamelCaps 。 So rename them to TopSection and BottomSection .所以将它们重命名为TopSectionBottomSection

Also check if your class has been imported - if bottomsection is underlined then try Alt+Enter and select Import Class还要检查您的课程是否已导入 - 如果bottomsection有下划线,则尝试Alt+Enter并选择导入课程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 必需的android.support.v4.app.fragment - required android.support.v4.app.fragment 有没有一种方法可以将片段转换为android.support.v4.app.Fragment - Is there a way to convert fragment to android.support.v4.app.Fragment 在实现FragmentTabHost时将android.support.v4.app.Fragment强制转换为android.app.Fragment的可能性 - Possibility of casting android.support.v4.app.Fragment to android.app.Fragment when implementing a FragmentTabHost 无法从android.support.v4.app.Fragment转换为android.app.Fragment - Cannot convert from android.support.v4.app.Fragment to android.app.Fragment 在同一活动中使用android.support.v4.app.Fragment和android.app.Fragment - Use android.support.v4.app.Fragment and android.app.Fragment in same activity 类型不匹配:无法从android.support.v4.app.Fragment转换为android.app.Fragment - Type mismatch: cannot convert from android.support.v4.app.Fragment to android.app.Fragment 使用backstack时android.app.Fragment和android.support.v4.app.Fragment的不同行为 - Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack android.support.v4.app.fragment和androidx.fragment.app.FragmentActivity有什么区别 - What is the difference between android.support.v4.app.fragment and androidx.fragment.app.FragmentActivity FragmentPagerAdapter android.support.v4.app.fragment无法应用于android.ap.Fragment - FragmentPagerAdapter android.support.v4.app.fragment can not be applied to android.ap.Fragment 原因:android.support.v4.app.Fragment $ InstantiationException:无法实例化片段 - Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM