Android片段错误

Android片段错误,android,android-fragments,Android,Android Fragments,我的问题在于,我在他身上画红线并给我写信: 覆盖“android.support.v4.app.Fragment”中不推荐的方法 请帮助我理解我做错了什么? 谢谢你所有的助手 package com.example.omermalka.memecreator; import android.app.Activity; import android.support.v4.app.Fragment; import android.os.Bundle; import

我的问题在于,我在他身上画红线并给我写信:

覆盖“android.support.v4.app.Fragment”中不推荐的方法

请帮助我理解我做错了什么? 谢谢你所有的助手

package com.example.omermalka.memecreator;

    import android.app.Activity;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    /**
     * Created by omermalka on 14/11/2015.
     */



    public class TopSectionFragment extends Fragment {

        private static EditText TopText;
        private static EditText BottomText;

        TopSectionListener acitivtyCommander;

        public interface TopSectionListener{
            public void createMime(String top , String Bottom);
        }


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

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.top_section_fragment, container, false);

            TopText = (EditText) view.findViewById(R.id.TopTextInput);
            BottomText = (EditText) view.findViewById(R.id.BottomTextInput);
            final Button button = (Button) view.findViewById(R.id.BottomTextInput);

            button.setOnClickListener(
                    new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            buttonClicked(v);
                        }
                    }
            );



            return view;
    }


        public void buttonClicked(View v) {
            acitivtyCommander.createMime(TopText.getText().toString(),BottomText.getText().toString());
        }

    }
你需要改变

public void onAttach(Activity activity)

最终代码:


看这个:我改变了它,现在我在附件上的活动上有了错误,谢谢你的帮助@神秘的为什么应用程序会崩溃,如果super.context;评论。在片段的情况下,如何决定是否需要调用超类生命周期方法。在活动的情况下,我知道它是强制的,否则会正确抛出异常
public void onAttach(Context context)
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
       acitivtyCommander = (TopSectionListener) context;
    } catch (ClassCastException e){
        throw new ClassCastException(context.toString());
    }
}