1

i have problem on my on Attach its doing red line on him and writing me :

"overrides deprecated method in 'android.support.v4.app.Fragment' "

Please help me understand what i am doing wrong ? ty you for all the helpers !!

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());
        }

    }
omer malka
  • 144
  • 1
  • 14

1 Answers1

2

You need to change

public void onAttach(Activity activity)

to

public void onAttach(Context context)

Final code:

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
       acitivtyCommander = (TopSectionListener) context;
    } catch (ClassCastException e){
        throw new ClassCastException(context.toString());
    }
}
Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • i change it and now i have error on the activity on the attach – omer malka Nov 14 '15 at 21:37
  • ty for all your helping !! – omer malka Nov 14 '15 at 21:41
  • @MysticaL Why the app will crash if `super.onAttach(context);` is commented. How to decide whether call to super class lifecycle method(s) is required or not in case of fragments. In case of Activities, I know its a compulsion(else exception is thrown right) – AnV Oct 02 '16 at 12:52