여전히 비교적 새로운데, 내 활동 클래스 MainActivity에서 사용하고있는 비 활동 클래스 MyLocation에서보기를 찾는 데 문제가 있습니다. 나는 경도와 위도를 얻기 위해 MyLocation을 사용하고있다. GPS 또는 네트워크가 사용 된 경우 텍스트보기를 강조 표시하려고합니다. 그렇게하려면 비 활동 클래스 인 MyLocation에서 textviews를 찾아야합니다. 나는 textviews을 찾기 위해 MyLocation에서 재판을 여기에서비 활동 클래스의 findViewById
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);
}
그리고 :
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) {
}
};
그러나보기는 발견되지 여기
내가 MainActivity에 전화 해요 방법이다. 이미 NPE @.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
이 있습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
여기서'getLocation' 메소드를 정의 했습니까? –
매우 간단 ... MyLocation 클래스를 호출 할 때 ... 매개 변수로 컨텍스트를 추가하면 거기에서 모든 것을 얻을 수 있습니다. – GvSharma