Application giving Crash on a button within a custom AlertDialog

0

I'm working with Alert Dialogs and I had an error where the application stopped responding. The following code refers to a snippet where the Google Maps API is being used, which means that when I click on a specific marker it will open an AlertDialog. So far so good, the problem starts when I try to reference an AlertDialog XML button, with id FinishAdd , but when I put these functions when the GPSActivity (the Fragment container) starts, the application "Crasha". Thank you for any response that might be useful to these functions. It's worth remembering that I'm using Android Studio, and the problem is related to an "OnClickListener" within an AlertDiolog.

Java:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {

            GPSActivity.ShowToast(marker.getTitle());

            if(marker.getTitle().contains("Inserir")) {
                GPSActivity.finLatLngGet = marker.getPosition();

                AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
                View mView = getActivity().getLayoutInflater().inflate(R.layout.dialog_window, null);

                mBuilder.setView(mView);
                AlertDialog dialog = mBuilder.create();
                Log.e("Activity", getActivity().getComponentName().toString());
                dialog.show();
//Erro à partir da linha abaixo
                Button btn_fin = (Button)getActivity().findViewById(R.id.concluirAdd);
                btn_fin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GPSActivity.ShowToast("Deu Certo");
                    }
                });
            }
            return false;
        }
    });

XML:     

<TextView
    android:id="@+id/txtTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Adicionar Marcador"
    android:textAlignment="center"
    android:layout_marginTop="5dp"
    android:textSize="20sp" />

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Título"
    android:inputType="textPersonName"
    android:textAlignment="center"
    android:layout_marginTop="14dp"
    android:layout_below="@+id/txtTitle"
    android:layout_alignParentLeft="true" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Mensagem"
    android:inputType="textPersonName"
    android:textAlignment="center"
    android:layout_below="@+id/editText"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="11dp" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="142dp"
    android:layout_height="50dp"
    android:layout_below="@+id/editText2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:entries="@array/event_name"
    android:textAlignment="center"
    android:layout_gravity="center"
    />

<Button
    android:id="@+id/concluirAdd"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:text="Finalizar" />

Error:

  

E / AndroidRuntime: FATAL EXCEPTION: main                                                                      Process: com.axis.appname, PID: 26078                                                                      java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener (android.view.View $ OnClickListener)' on a null object reference                                                                          at com.axis.appname.MapFragment $ 1.onMarkerClick (MapFragment.java:266)                                                                          at com.google.android.gms.maps.GoogleMap $ 2.zza (Unknown Source)                                                                          at com.google.android.gms.maps.internal.zzu $ zza.onTransact (Unknown Source)                                                                          at android.os.Binder.transact (Binder.java:499)                                                                          at com.google.android.gms.maps.internal.bz.a (: com.google.android.gms.DynamiteModulesB: 84)                                                                          at com.google.maps.api.android.lib6.impl.dd.b (: com.google.android.gms.DynamiteModulesB: 292)                                                                          at com.google.maps.api.android.lib6.gmm6.api.e.a (: com.google.android.gms.DynamiteModulesB: 242)                                                                          at com.google.maps.api.android.lib6.gmm6.vector.m.a (: com.google.android.gms.DynamiteModulesB: 4070)                                                                          at com.google.maps.api.android.lib6.gmm6.vector.af.c (: com.google.android.gms.DynamiteModulesB: 611)                                                                          at com.google.maps.api.android.lib6.gmm6.vector.df.onSingleTapConfirmed (: com.google.android.gms.DynamiteModulesB: 236)                                                                          at com.google.maps.api.android.lib6.impl.gesture.g.onSingleTapConfirmed (: com.google.android.gms.DynamiteModulesB: 189)                                                                          at com.google.maps.api.android.lib6.impl.gesture.i.handleMessage (: com.google.android.gms.DynamiteModulesB: 132)                                                                          at android.os.Handler.dispatchMessage (Handler.java:102)                                                                          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)

    
asked by anonymous 22.06.2017 / 19:13

1 answer

1

Instead of getActivity() use View . Here's an example:

Button btn_fin = (Button) mView.findViewById(R.id.concluirAdd);
    
22.06.2017 / 20:07