简体   繁体   中英

Fragment implenets View.OnClickListener - button doesn't work

Why doesn't my button show toast message and writes to the log. The button is inside the fragment?

Code inside my MainActivity.java

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

Code inside my FragmentA.java:

    public class FragmentA extends Fragment implements   View.OnClickListener{

    Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
    }

    @Override
    public void onClick(View view) {
        Toast.makeText(getActivity(), "Do it", Toast.LENGTH_LONG).show();
        Log.w("myApp","Hi there");
    }
}

Code inside fragment_a.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFBB00"
    >


    <Button
        android:layout_margin="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        />

</LinearLayout>

Code inside activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BBFF"
    >


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.audiorecbas.audio_recorder_basic.FragmentA"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        button = (Button)getActivity().findViewById(R.id.button);
        button.setOnClickListener(this);
    }

In your Fragment Java class, you need to have the following to handle the Button's click event: Like this way

    public class FragmentA extends Fragment implements   View.OnClickListener
    {
           Button btn;

         @Override
         public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);

                btn = (Button) getActivity().findViewById(R.id.button);
                btn.setOnClickListener(this);       

            ......    
            }

For better information please check SO ANSWER

You haven't linked the button's onClickListener after instantiating it.

button = (Button) getActivity.findViewById(R.id.button);
button.setOnClickListener(this); //add this

Remove this entire code

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    button = (Button)getActivity().findViewById(R.id.button);

}

then add

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_a, container, false);
    button= (Button) view.findViewById(R.id.button);
    button.setOnClickListener(this);
    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            {
             //Do your stuff here
            }
         }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM