FindViewById in class with no activity

Still relatively new to this, I am having trouble finding a view in the inactive MyLocation class that I use in my MainActivity activity class. I use MyLocation to get longitude and latitude. I want to highlight text when using GPS or network. To do this, I need to find text views in the MyLocation inactivity class.

This is what I call it in 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); } 

And here is what I tried to find in MyLocation in the text:

 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) { } }; 

But no views were found. I already got NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE); . What am I doing wrong?

+7
android layout-inflater
source share
3 answers

get NPE @ .getSystemService (Context.LAYOUT_INFLATER_SERVICE) ;. What am I doing wrong?

Because context is null in the MyLocation class. use the constructor of the MyLocation class to pass the MainActivity context to MyLocation to access system services as follows:

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

and MainActivity create an object of the MyLocation class, passing the MainActivity context as follows:

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

Now use context to access system services in the MyLocation class

EDIT: Instead of inflating the main layout in onLocationChanged again, use the Activity context to access the view from the operation layout as:

  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); .... } 
+11
source share

Why do you want to do this in a separate class, since you implement the "LocationListener" in the activity itself. And if you want to do it in a separate class. You can notify about this by setting custom lists as

 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) { } }; } 
0
source share

Try this way, hope it helps you solve your problem.

 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) { } }; } 
0
source share

All Articles