Android Fragment onAttach错误

时间:2015-11-14 21:11:22

标签: 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.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());
        }

    }

1 个答案:

答案 0 :(得分:2)

您需要更改

document.getElementById

public void onAttach(Activity activity)

最终代码:

public void onAttach(Context context)