Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unreachable statement after using .getActivity( ) in a Fragment

I want to use .getSystemService in a Fragment. When i use .getActivity() to get the context of my activity, Android Studio tells me in the same line that this is a "unreachable statement".

When there is a line above the line where i use "getActivity()", it will show that this line on top is unreachable.

Why and how to fix this?

public class NewNodeFragment extends Fragment {

//GPS SIGNAL
double pLat;
double pLong;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.newnode_layout, container,false);

    //GPS SIGNAL
    LocationManager gpsmanager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    Location lastLocation = gpsmanager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (lastLocation != null) {
        pLat = lastLocation.getLatitude();
        pLong = lastLocation.getLongitude();
    }

    LocationListener gpslistener = new mylocationListener();
    gpsmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpslistener);
}
like image 468
iamrobin. Avatar asked Dec 01 '22 00:12

iamrobin.


1 Answers

You have a return statement as the first line in your method, right above the line that has your comment //GPS SIGNAL...

Anything after a return statement is of course unreachable code.

like image 163
Jim Avatar answered Dec 04 '22 12:12

Jim