Problem when showing Alertdialog

Asked

Viewed 148 times

0

I have an error message when trying to show Alertdialog using Android Studio.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registrar_pagamento_maos);
        searchView = findViewById(R.id.schvHistorico);
        lstSelecionaClientePag = findViewById(R.id.lstSelecionaClientePag);
        edtDataRecebimentoAct = findViewById(R.id.edtDataRecebimentoAct);
        edtValorMensalidadePag = findViewById(R.id.edtValorMensalidadePag);
        inicializarFirebase();
        progress = new ProgressDialog(RegistrarPagamentoMaos.this);
        progress.setTitle("Carregando");
        progress.setMessage("Sincronizando");
        progress.setCancelable(true);
        progress.show();

    arrayListFilho = new ArrayList<>();
    eventoDatabaseFiltroNomeFilho();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            eventoDatabase(newText);
            return false;
        }
    });
    lstSelecionaClientePag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            filhoSelecionado = (Filho) parent.getItemAtPosition(position);

            AlertDialog.Builder mBuilder = new AlertDialog.Builder(RegistrarPagamentoMaos.this);
            View mView = getLayoutInflater().inflate(R.layout.customdlg_cliente_selecionado, null);

            final EditText edtDataRecebimento = (EditText) mView.findViewById(R.id.edtDataRecebimento);
            final EditText edtValorRecebimento = (EditText) mView.findViewById(R.id.edtValorRecebimento);
            final Button btnCancelarPag = (Button) mView.findViewById(R.id.btnCancelarPag);
            final Button btnOkPag =  (Button) mView.findViewById(R.id.btnOkPag);
            final Button btnSelecionarData =  (Button) mView.findViewById(R.id.btnSelecionarData);



            edtDataRecebimento.setKeyListener(null);
            mBuilder.setView(mView);
            final AlertDialog dialog = mBuilder.create();
            dialog.show();

            btnSelecionarData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                 new DatePickerDialog(
                         RegistrarPagamentoMaos.this,listener, calendar.get(Calendar.YEAR),
                         calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)

                 ).show();

                }

                DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        data =(dayOfMonth+"/"+(month+1)+"/"+year);
                        try
                        {
                            Date date = new SimpleDateFormat("dd/MM/yyyy").parse(data);
                            data = new SimpleDateFormat("dd/MM/yyyy").format(date);


                        }
                        catch (ParseException e)
                        {
                            e.printStackTrace();
                        }
                        edtDataRecebimento.setText(data);
                        SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
                        try {
                            Date parseDate = f.parse(data);
                            long miliseconds = parseDate.getTime();
                            edtDataRecebimentoAct.setText(String.valueOf(miliseconds));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }

                    }

                };
            });


            btnCancelarPag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            btnOkPag.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Pagamento p = new Pagamento();
                    p.setUidcontratante(filhoSelecionado.getUid());
                    p.setUidhistorico(UUID.randomUUID().toString());
                    p.setDatapagamento(edtDataRecebimentoAct.getText().toString());
                    p.setValorpago(Double.valueOf(edtValorRecebimento.getText().toString()));


                    databaseReference.child("Pagamento").child(filhoSelecionado.getUidresponsavel()).child("HistoricoPagamento").child(p.getUidhistorico()).setValue(p);
                    dialog.dismiss();

                }

            });


        }

});

}` 

And this is the layout of my Custom Alertdialog

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="Registrar Pagamento"
        android:textAlignment="center"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Data"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <EditText
        android:id="@+id/edtDataRecebimento"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="date"
        android:textAlignment="center"
        app:layout_constraintEnd_toStartOf="@+id/btnSelecionarData"
        app:layout_constraintHorizontal_bias="0.6"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <Button
        android:id="@+id/btnSelecionarData"
        style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="selecionar data"
        app:layout_constraintEnd_toEndOf="@+id/textView4"
        app:layout_constraintStart_toEndOf="@+id/edtDataRecebimento"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="15dp"
        android:layout_marginTop="8dp"
        android:text="Valor Recebido"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="@+id/btnSelecionarData"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/edtDataRecebimento"
        app:layout_constraintTop_toBottomOf="@+id/edtDataRecebimento" />

    <EditText
        android:id="@+id/edtValorRecebimento"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="@+id/textView5"
        app:layout_constraintStart_toStartOf="@+id/textView5"
        app:layout_constraintTop_toBottomOf="@+id/textView5" />

    <Button
        android:id="@+id/btnOkPag"
        style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="@android:string/ok"
        app:layout_constraintEnd_toEndOf="@+id/edtValorRecebimento"
        app:layout_constraintTop_toBottomOf="@+id/edtValorRecebimento" />


    <Button
        android:id="@+id/btnCancelarPag"
        style="@style/Widget.AppCompat.Button.ButtonBar.AlertDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Cancelar"
        app:layout_constraintStart_toStartOf="@+id/edtValorRecebimento"
        app:layout_constraintTop_toBottomOf="@+id/edtValorRecebimento" />

</android.support.constraint.ConstraintLayout>

The problem is when I click to get my Alertdialog return me this error message:

Process: com.luped.mescolar, PID: 30040
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
        at android.support.v7.app.AlertController.setupView(AlertController.java:468)
        at android.support.v7.app.AlertController.installContent(AlertController.java:233)
        at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:395)
        at android.app.Dialog.show(Dialog.java:294)
        at com.luped.mescolar.RegistrarPagamentoMaos$2.onItemClick(RegistrarPagamentoMaos.java:100)
        at android.widget.AdapterView.performItemClick(AdapterView.java:310)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121)
        at android.widget.AbsListView$3.run(AbsListView.java:4036)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I/art: Background sticky concurrent mark sweep GC freed 20364(1347KB) AllocSpace objects, 13(260KB) LOS objects, 24% free, 5MB/7MB, paused 11.914ms total 586.043ms
V/FA: Activity resumed, time: 115005878
Application terminated.

1 answer

0


You can use DialogFragment. To call, just use:

BlankFragment.show("testando",getSupportFragmentManager());

In the method item parameter show(), can pass data if you need.

public class BlankFragment extends DialogFragment {
    EditText edtDataRecebimento;
    EditText edtValorRecebimento;
    Button btnOkPag;
    Button btnSelecionarData;
    Button btnCancelarPag;

public static void show(String item,FragmentManager fm) {

    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("teste");
    if (prev != null) {
        ft.remove(prev);
    }

    ft.addToBackStack(null);
    BlankFragment frag = new BlankFragment();
    Bundle args = new Bundle();
    args.putString("item", item);
    frag.setArguments(args);
    frag.show(fm,"t");
}

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

    View mView = inflater.inflate(R.layout.fragment_blank, container, false);

    edtDataRecebimento  = (EditText) mView.findViewById(R.id.edtDataRecebimento);
    edtValorRecebimento  = (EditText) mView.findViewById(R.id.edtValorRecebimento);
    btnCancelarPag  = (Button) mView.findViewById(R.id.btnCancelarPag);
    btnOkPag  =  (Button) mView.findViewById(R.id.btnOkPag);
    btnSelecionarData  =  (Button) mView.findViewById(R.id.btnSelecionarData);

    String string = getArguments().getString("item");
    edtDataRecebimento.setText(string);

    btnSelecionarData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar calendar = Calendar.getInstance();

            new DatePickerDialog(
                    getContext(),listener, calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)

            ).show();

        }

        DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String data =(dayOfMonth+"/"+(month+1)+"/"+year);
                try
                {
                    Date date = new SimpleDateFormat("dd/MM/yyyy").parse(data);
                    data = new SimpleDateFormat("dd/MM/yyyy").format(date);


                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                }
                edtDataRecebimento.setText(data);
                SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
                try {
                    Date parseDate = f.parse(data);
                    long miliseconds = parseDate.getTime();
                    //edtDataRecebimentoAct.setText(String.valueOf(miliseconds));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }

        };
    });
    btnCancelarPag.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
    getDialog().setTitle(string);

    return mView;
}

}

Browser other questions tagged

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