Skip to main content
3 de 3
se añadieron 1396 caracteres en el cuerpo
Mark Dev
  • 447
  • 7
  • 16

¿por que tras cerrar un fragment la activdad principal me marcar error app?

Por raro que parezca, tengo un dialog fragment que cierro con dismiss(), pero este fragment despliego info (Coordenadas, calle, ciudad y pais), les pongo código y el error que marca :

Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference

y el error donde marca es la siguiente linea del fragment:

...
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
...

y el otra parte:

this.df_ubicame.setLocation(loc);

y todo el código de mi DialogFragment:

public class DF_Ubicame extends DialogFragment {

TextView txv_ubicame, txv_colonia;
ImageButton fab_close;

public DF_Ubicame(){  }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    return inflater.inflate(R.layout.df_ubicame, container, false);

}

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(85,15, 15, 15)));

    CastControles(view);


    if (savedInstanceState != null) {
        txv_ubicame.setText(savedInstanceState.getString("ubicacion"));
        txv_ubicame.setText(savedInstanceState.getString("direccion"));
    }


    LocationManager mlocManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    Localizacion Local = new  Localizacion();
    Local.setDf_ubicame(this);
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);


    txv_ubicame.setText("Ubicacion");
    txv_colinia.setText("Ubicacion");
    fab_close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           dismiss();
        }
    });

}

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("ubicacion", txv_ubicame.getText().toString());
    outState.putString("direccion", txv_colinia.getText().toString());

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


}

private void CastControles(View v) {
    txv_ubicame = (TextView) v.findViewById(R.id.txv_ubicame);
    txv_colinia = (TextView) v.findViewById(R.id.txv_colinia);
    fab_close = (ImageButton) v.findViewById(R.id.fab_close);

}

public void setLocation(Location loc) {

    if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
        try {
            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            List<Address> list = geocoder.getFromLocation(
                    loc.getLatitude(), loc.getLongitude(), 1);
            if (!list.isEmpty()) {
                Address DirCalle = list.get(0);
                txv_colinia.setText("Mi direccion es: \n"
                        + DirCalle.getAddressLine(0) + ",\n"
                        + DirCalle.getAddressLine(1) + ",\n"
                        + DirCalle.getAddressLine(2) + ",\n"
                        + DirCalle.getAddressLine(3) + " \n");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public class Localizacion implements LocationListener {
    DF_Ubicame df_ubicame;

    public DF_Ubicame getDf_ubicame() {
        return df_ubicame;
    }

    public void setDf_ubicame(DF_Ubicame df_ubicame) {
        this.df_ubicame = df_ubicame;
    }

    @Override
    public void onLocationChanged(Location loc) {

        loc.getLatitude();
        loc.getLongitude();

        String Text = "Mi ubicacion actual es: " + "\n" +
                "Latitud: " + loc.getLatitude() + "\n" +
                "Longitud: " + loc.getLongitude();
        txv_ubicame.setText(Text);
        this.df_ubicame.setLocation(loc);
    }

    @Override
    public void onProviderDisabled(String provider) {
        txv_ubicame.setText("GPS Desactivado");
    }

    @Override
    public void onProviderEnabled(String provider) {
        txv_ubicame.setText("GPS Activado");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
      
       
    }

}

alguna idea de por que sucede ese error???

adjunto el log:

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
                  at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
                  at android.location.Geocoder.<init>(Geocoder.java:83)
                  at com.mark.exportadora.DF_Ubicame.setLocation(DF_Ubicame.java:112)
                  at com.mark.exportadora.DF_Ubicame$Localizacion.onLocationChanged(DF_Ubicame.java:151)
                  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:290)
                  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:219)
                  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:235)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7231)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Mark Dev
  • 447
  • 7
  • 16