iam*_*in. 4 android fragment android-studio
我想在片段中使用.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);
}
Run Code Online (Sandbox Code Playgroud)
Jim*_*Jim 13
你有一个return语句作为你方法的第一行,就在你评论的行上方// GPS SIGNAL ...
返回语句后的任何内容当然都是无法访问的代码.
您必须将所有代码放在return语句之前.
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7123 次 |
| 最近记录: |