在非Activity类中findViewById

对此我还是比较新的,我在查找非活动类MyLocation中的问题时遇到了问题,该问题在我的活动类MainActivity中使用。 我使用MyLocation获取经度和纬度。 当使用GPS或networking时,我想突出显示一个文本视图。 为此,我需要在非活动类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) { } }; 

但没有find意见。 我已经得到了NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 。 我究竟做错了什么?

Solutions Collecting From Web of "在非Activity类中findViewById"

得到一个NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. 我究竟做错了什么?

由于MyLocation类中的contextnull 。 使用MyLocation类构造函数在MyLocation传递MainActivity上下文以访问系统服务,如下所示:

 Activity activity; public MyLocation(Context context,Activity activity){ this.context=context; this.activity=activity; } 

并在MainActivity通过传递MainActivity上下文来创buildMyLocation类对象:

 MyLocation myLocation = new MyLocation(MainActivity.this,this); 

现在使用context访问MyLocation类中的系统服务

编辑:而不是膨胀主布局onLocationChanged再次使用活动上下文来访问活动布局视图为:

  public void onLocationChanged(Location location) { .... tvnetwork = (TextView) activity.findViewById(R.id.tvnetwork); tvgps = (TextView) activity.findViewById(R.id.tvgps); defaultTextColor = tvgps.getTextColors().getDefaultColor(); tvnetwork.setTextColor(context.getResources().getColor( R.color.green)); tvgps.setTextColor(defaultTextColor); .... } 

为什么你要在单独的类中做,因为你正在活动本身中实现“LocationListener”。 如果你想在单独的课堂上做。 您可以通过设置像这样的自定义列表来通知活动

 public class MainActivity extends ActionBarActivity implements LocationListener,CustomListner { TextView tvnetwork, tvgps; private int defaultTextColor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvnetwork = (TextView) v.findViewById(R.id.tvnetwork); tvgps = (TextView) v.findViewById(R.id.tvgps); defaultTextColor = tvgps.getTextColors().getDefaultColor(); MyLocation myLocation = new MyLocation(this); // pass listner in constructor myLocation.getLocation(this, locationResult); } @Override public void highlight(){ // update your view here. tvnetwork.setTextColor(context.getResources().getColor( R.color.green)); tvgps.setTextColor(defaultTextColor); } public class MyLocation { LocationManager lm; LocationResult locationResult; CustomListner listner; MyLocation(CustomListner listner){ this.listner = listner; } public interface CustomListner{ public void highlight(); } LocationListener locationListenerNetwork = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location); listner.highlight(); 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) { } }; } 

试试这样,希望这会帮助你解决你的问题。

 public class MainActivity extends ActionBarActivity implements LocationListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyLocation myLocation = new MyLocation(this); myLocation.getLocation(this, locationResult); } } public class MyLocation { LocationManager lm; LocationResult locationResult; private Context context; TextView tvnetwork, tvgps; private int defaultTextColor; public void MyLocation(Context context) { this.context = context; } LocationListener locationListenerNetwork = new LocationListener() { public void onLocationChanged(Location location) { locationResult.gotLocation(location); View v = LayoutInflater.from(context).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) { } }; }