在片段中使用.getActivity()后无法访问的语句 [英] unreachable statement after using .getActivity( ) in a Fragment
问题描述
我想在片段中使用.getSystemService。当我使用.getActivity()获取我的活动的上下文时,Android Studio告诉我,这是一个无法访问的语句。
在我使用getActivity()的行的上面一行,它会显示这条行在顶部是无法访问的。
为什么以及如何解决这个问题?
public class NewNodeFragment extends Fragment {
// GPS信号
double pLat;
double pLong;
@Override
public view onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.newnode_layout,container,false);
// GPS信号
LocationManager gpsmanager =(LocationManager)getActivity()。getSystemService(Context.LOCATION_SERVICE);
位置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);
你的方法的第一行,就在你的注释// GPS信号的行的上方......
返回语句之后的任何东西当然是无法访问的代码。 p>
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);
}
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.
这篇关于在片段中使用.getActivity()后无法访问的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!