我想在片段中使用 .getSystemService 。当我使用 .getActivity() 获取活动的上下文时,Android Studio 在同一行中告诉我这是一个“无法访问的语句”。
当我使用“getActivity()”的行上方有一行时,它将显示顶部的这一行无法访问。
为什么以及如何解决这个问题?
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);
}
您的方法中的第一行有一个 return 语句,位于注释 //GPS SIGNAL... 的行的正上方。
return 语句之后的任何内容当然都是无法访问的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)