作为一个相对新手,我在非活动类MyLocation中找不到视图,在我的活动类MainActivity中使用MyLocation来获取经纬度。我想在GPS或网络被使用时突出显示一个TextView。为此,我需要在非活动类MyLocation中找到这些TextView。
以下是我在MainActivity中的调用方式:
public class MainActivity extends ActionBarActivity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
以下是我在MyLocation尝试查找文本视图的内容:
public class MyLocation {
LocationManager lm;
LocationResult locationResult;
private Context context;
TextView tvnetwork, tvgps;
private int defaultTextColor;
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
locationResult.gotLocation(location);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main, null);
tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
tvgps = (TextView) v.findViewById(R.id.tvgps);
defaultTextColor = tvgps.getTextColors().getDefaultColor();
tvnetwork.setTextColor(context.getResources().getColor(
R.color.green));
tvgps.setTextColor(defaultTextColor);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
但是视图没有被找到。我在使用.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
时已经收到了NPE错误提示。我做错了什么?
getLocation
方法? - Spring Breaker