Button in Fragment android

Asked

Viewed 627 times

0

I own a Fragment and this fragment, has a Floatingactionbutton.

I invoke the method onClickListener:

FloatingActionButton floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fabAddEventos);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            adicionarEvento(v);
        }
    });

When clicking, I invoke the method to add Wind passing the View as parameter to create a Dialog with several fields of Edit Text and Button.

public void adicionarEvento(View view) {
    Dialog dialog = new Dialog(view.getContext());
    EditText etEventos_nome = (EditText) view.findViewById(R.id.etEventos_nome);
    Button btn_eventos_confirmar = (Button) view.findViewById(R.id.btn_eventos_confirmar);
    btn_eventos_confirmar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

The error occurs in the Context or View Dialog, locking the application.

  • Ta very confused your question, what is the mistake? Put it here also

  • I re-asked the question. No error, however, crashes the app when invoking the setOnClickListener() de Button. In short, the logic is as follows: A Fragment has a Floatingactionbutton that opens a Dialog, the latter has a Button.

  • Try to do this: Dialog dialog = new Dialog(getActivity()); in place of Dialog dialog dialog = new Dialog(view.getcontext());, for that view getcontext() is taking the context of the method, not the class

  • I edited my answer below, see if it solves your problem.

2 answers

1


fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                dialogFragment = new DialogExample();
                fragmentManager = getSupportFragmentManager();
                dialogFragment.show(fragmentManager,"");
            }
        });

Class Dialogexample

public class DialogExample extends DialogFragment {

private static View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.dialog_example, null);

    int width = getResources().getDimensionPixelSize(R.dimen.popup_width);
    int height = getResources().getDimensionPixelSize(R.dimen.popup_height);
    getDialog().getWindow().setLayout(width, height);

    EditText etEventos_nome = (EditText) view.findViewById(R.id.etEventos_nome);
    Button btn_eventos_confirmar = (Button) view.findViewById(R.id.btn_eventos_confirmar);
    btn_eventos_confirmar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    return view;


}

}

xml dialog_example

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/btn_eventos_confirmar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <EditText
        android:id="@+id/etEventos_nome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="aaaaa" />
</LinearLayout>

  • What your answer has to do with his need ?

1

void onClick(View v)

View v: Represents the view that was clicked, in this case it would be like you were calling:

adicionarEventos(floatActionButton);

and then you try to take a view in Fab with an id that doesn’t exist

EditText etEventos_nome = (EditText) floatActionButton.findViewById(R.id.etEventos_nome);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.