在 Fragment 中使用 .getActivity( ) 后无法访问语句
unreachable statement after using .getActivity( ) in a Fragment
我想在 Fragment 中使用 .getSystemService。当我使用 .getActivity() 获取我的 activity 的上下文时,Android Studio 在同一行中告诉我这是一个 "unreachable statement".
当我使用"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 语句之后的任何内容当然是无法访问的代码。
您必须将所有代码放在 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);
}
我想在 Fragment 中使用 .getSystemService。当我使用 .getActivity() 获取我的 activity 的上下文时,Android Studio 在同一行中告诉我这是一个 "unreachable statement".
当我使用"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 语句之后的任何内容当然是无法访问的代码。
您必须将所有代码放在 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);
}