简体   繁体   中英

Button click event in a Fragment

First of all I want to say that I am studying the Android documentation, so I don't have much knowledge about this topic so far. This is the code I have written:

public class Android extends Fragment {
    @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View prime = inflater.inflate(R.layout.android_frag, container, false);

            //I have a spinner with multiple choices here. The Spinner has the id 'convtype'
            Spinner spinner = (Spinner) prime.findViewById(R.id.convtype);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(),
                    R.array.planets_array, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);

            return prime;
         }

        private String getGradi(String a) {     
            return a+"°";
        }

         //This method is called when I click a button
         public void convertStart(View v) {

             Spinner spinner = (Spinner) v.findViewById(R.id.convtype);
             int indice = spinner.getSelectedItemPosition();
             EditText mEdit = (EditText) v.findViewById(R.id.unita);

             String gradi = getGradi("65");
             mEdit.setText(gradi);

         }

}

And below you can find the XML part about the button I have created in the app:

<Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/calcola"
        android:onClick="convertStart" />

This code, from what I have understood, should be correct but when I try to run the app in my emulator an error is raised and it tells me that unfortunately my app has stopped.

I guess that convertStart(View v) has the v parameter which is used inside the method but in the android:onClick I am not passing any argument. Any suggestion?

The problem is that the function you reference in the android:onClick XML attribute must reside in the View's Context . A Fragment is not a Context , so Android is not looking in your Fragment for the convertStart(View) method.

This is documented in the android:onClick documentation, but that doesn't make it particular clear that "context" really means a Context .

You need to put convertStart() in your Activity if you want to use the XML attribute. Alternatively, you can call setOnClickListener() on the View in your Fragment to create a callback in the Fragment.

I prefer the latter method myself, as I find it much easier to follow the logical flow of an application that way. It is also less prone to errors that could occur from innocent actions such as renaming the method or trying to use this layout somewhere else.

put this on onCreateView method before return prime

final Button button = (Button) prime.findViewById(R.id.button1);
     button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });

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