Search code examples
androidnullpointerexceptioncustomdialog

Nullpointerexception after press dialog button android


I have a custom dialog inside a fragment, the problem its that a cant programm the button, each time that i programm the button and i run the programm i get nullpointerexception error, could someone helpme please, here its the code:

class which call the customdialog:

public class PruebaUsuario extends Fragment implements OnMapReadyCallback {

    private SupportMapFragment sMapFragment;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private Button mPedir;
    private Button mConfirmar;


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

        //conseguir usuario actual
        // mUsuarioActual = ParseUser.getCurrentUser();

        //preparar google maps
        final View x = inflater.inflate(R.layout.prueba_usuario, container, false);
        mPedir=(Button)x.findViewById(R.id.pedirBtn);


        sMapFragment = SupportMapFragment.newInstance();
        sMapFragment.getMapAsync(this);
        android.support.v4.app.FragmentManager fm = getFragmentManager();
        android.support.v4.app.FragmentManager sFm = getChildFragmentManager();
        sFm.beginTransaction().add(R.id.mapa, sMapFragment).commit();

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

                Dialog dialogo = new Dialog(getContext());
                dialogo.setTitle("Confirmacion");
                dialogo.setContentView(R.layout.confirmacion_pedido);
                dialogo.show();
                mConfirmar=(Button) x.findViewById(R.id.confirmarBtn2);
                mConfirmar.setEnabled(false);


            }
        });



        return x;

    }


    @Override
    public void onMapReady(GoogleMap googleMap) {

        if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        googleMap.setMyLocationEnabled(true);


        LocationManager mng = (LocationManager) getContext().getSystemService(getContext().LOCATION_SERVICE);
        Location location1 = mng.getLastKnownLocation(mng.getBestProvider(new Criteria(), false));
        double lat =  location1.getLatitude();
        double lon = location1.getLongitude();
        LatLng ubicacion = new LatLng(lat, lon);
        float zoomLevel = 14;
        googleMap.addMarker(new MarkerOptions().position(ubicacion).title("Marker in Sydney"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ubicacion, zoomLevel));







    }


}

xml from the class:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pedir Taxi"
        android:id="@+id/pedirBtn" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mapa"
        tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />
</LinearLayout>

xml from the custom dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Por favor confirme datos direccion"
        android:id="@+id/textView5" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="94dp"
        android:weightSum="1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="198dp"
            android:layout_height="144dp"
            android:layout_weight="0.38">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText2" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="150dp"
            android:layout_height="148dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText3" />
        </LinearLayout>
    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText4"
        android:layout_gravity="center_horizontal"
        android:hint="Ej: independendcia y avellaneda, Frente a carlos v"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Confirmar Pedido"
            android:id="@+id/confirmarBtn2"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="79dp" />
    </RelativeLayout>

</LinearLayout>

Solution

  • Change the code like as below code for your dialog and check:

    Dialog dialogo = new Dialog(getActivity()); //change getContext to getActivity
    dialogo.setTitle("Confirmacion");
    dialogo.setContentView(R.layout.confirmacion_pedido);
    dialogo.show();
    mConfirmar=(Button) dialogo.findViewById(R.id.confirmarBtn2); //instead of x use dialogo
    mConfirmar.setEnabled(false);