Application giving Crash on a button inside a custom Alertdialog

Asked

Viewed 45 times

0

I am working with Alert Dialogs and I appeared an error where the application stops responding. The following code refers to a fragment where you are using the Google Maps API, and means that when I click on a specific marker it will open a Alertdialog. So far so good, the problem starts when I try to reference an XML button of Alertdialog, with the id Concluiradd, However, when placing these functions, when Gpsactivity (the Fragment container) is started, the application "Crasha". I would like to thank you for any response that might be of use to these tasks. Remember that I am using Android Studio, and the problem is related to an "Onclicklistener" inside a 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)

1 answer

1


In place of getActivity() use the View. Take an example:

Button btn_fin = (Button) mView.findViewById(R.id.concluirAdd);

Browser other questions tagged

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