why after closing a fragment the main activity marked me error app?

1

Oddly enough, I have a dialog fragment that I close with dismiss (), but this fragment I display info (coordinates, street, city and country), I put code and the error that marks:

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

and the error where it marks is the following fragment line:

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

and the other part:

this.df_ubicame.setLocation(loc);

and all the code of my 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) {


    }

}

Any idea why this error happens ???

attached the 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)
    
asked by Mark Dev 17.01.2017 в 02:06
source

1 answer

0

The Error was that the class that I use remains in execution while the fragment is already closed; add apparently very unorthodox an if it validates if my fragment dialog is visible:

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();

    if(df_ubicame.isVisible()){ /* la solucion*/
            txv_ubicame.setText(Text);
            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) {


}

}

    
answered by 20.01.2017 в 22:31