Button in Fragment android

0

I have a Fragment and this fragment has a FloatingActionButton .

I call the onClickListener method:

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

            adicionarEvento(v);
        }
    });

When I click, I invoke the addEvent method by passing the View as a parameter to create a Dialog with several Edit Text and Button fields. / p>

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 of Dialog, crashing the application.

    
asked by anonymous 27.09.2017 / 19:55

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>

    
27.09.2017 / 20:31
1

void onClick(View v)

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

adicionarEventos(floatActionButton);

Then you try to get a view in fab with an id that does not exist

EditText etEventos_nome = (EditText) floatActionButton.findViewById(R.id.etEventos_nome);
    
29.09.2017 / 18:52