unreachable statement after using .getActivity( ) in a Fragment

后端 未结 2 1350
花落未央
花落未央 2021-01-03 08:03

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 \"unreach

相关标签:
2条回答
  • 2021-01-03 08:32

    You would have to put all your code before the return statement.

    public class NewNodeFragment extends Fragment {
    
    //GPS SIGNAL
    double pLat;
    double pLong;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
    
        //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);
    
        return inflater.inflate(R.layout.newnode_layout, container,false);
    }
    
    0 讨论(0)
  • 2021-01-03 08:44

    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.

    0 讨论(0)
提交回复
热议问题