【发布时间】:2014-11-06 13:39:13
【问题描述】:
对此我仍然是相对较新的我在我的活动类 MainActivity 中使用的非活动类 MyLocation 中查找视图时遇到问题。我正在使用 MyLocation 来获取经度和纬度。 我想在使用 GPS 或网络时突出显示文本视图。为此,我需要在非活动类 MyLocation 中找到文本视图。
这是我在 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) {
}
};
但未找到视图。我已经获得了 NPE @.getSystemService(Context.LAYOUT_INFLATER_SERVICE);。我做错了什么?
【问题讨论】:
-
你在哪里定义了
getLocation方法? -
非常简单...当您调用 MyLocation 类时...只需将上下文作为参数添加到它,这样您就可以从那里获取所有内容。