findViewById in non-Activity class - android

Still being relatively new to this I'm having issues in finding a view in a non-activity class MyLocation which I'm using in my activity class MainActivity. I'm using MyLocation to get longitude and latitude.
I want to highlight a textview when either GPS or Network has been used. To do so I need to find the textviews in the non-activity class MyLocation.
Here is how I'm calling 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 what I tried in MyLocation to find the textviews:
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 the views are not found. I already get a NPE # .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong?

get a NPE # .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What
am I doing wrong?
Because context is null in MyLocation class. use MyLocation class constructor to pass MainActivity context in MyLocation to access system services as:
Activity activity;
public MyLocation(Context context,Activity activity){
this.context=context;
this.activity=activity;
}
and in MainActivity create MyLocation class object by passing MainActivity context as:
MyLocation myLocation = new MyLocation(MainActivity.this,this);
Now use context to access system services in MyLocation class
EDIT: Instead of inflating main layout again in onLocationChanged use Activity context to access view from Activity 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);
....
}

Try this way,hope this will help you to 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) {
}
};
}

Why you want to do it in separate class as your are implementing "LocationListener" in Activity itself. And if you want to do in separate class. You can notify the activity by setting custom listners like this
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) {
}
};
}

Related

LocationListener how to stop when onLocationChanged() is called?

I want locationlistener to stop using gps when the location is changed, is this possible ?
public class MyActivity extends AppCompatActivity implements LocationListener {
private LocationManager locationManager;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
here this is the reference to LocationListener Interface.
If you want only single location update then you can call
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, Looper.getMainLooper());

hi I am getting error "cannot resolve method locationmanager.requestLocationUpdates();

Sir,
thanks for your help, here is the complete code of my program.
hi I am getting error "cannot resolve method locationmanager.requestLocationUpdates();
public class MainActivity extends Activity implements LocationListener {
public Button button;
private TextView textView;
public LocationManager locationmanager;
Context context;
private double lat,lon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationmanager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//error !!!!!!
**Error ** locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,0,this);
}
});
}
#Override
public void onLocationChanged(Location location) {
if(location!=null)
{
lat=location.getLatitude();
lon=location.getLongitude();
textView.append("\n lat:"+lat+" lon:"+lon);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
}
First, do not use "gps". Use LocationManager.GPS_PROVIDER.
Second, this does not match any signature for requestLocationUpdates(). this is a View.OnClickListener, not a LocationListener or a PendingIntent.
If this code is from an activity or fragment, and that activity or fragment is implementing LocationListener, this MyActivity.this instead of this, where MyActivity is the name of the activity or fragment that is implementing LocationListener.

Why wont my onClick work?

It seems like my onClick is malfunctioning, the text won't change when I click it and the button is still enabled after a click.
I'm trying to get a button that (when pressed by the user) updates the location.
Here's my code:
public class Search extends ActionBarActivity {
private double[][] values_gps = new double[100][1];
private Location location;
private TextView textLat;
private TextView textlong;
private Context mContext;
private double CurrentLatitude = 0;
private double CurrentLongitude = 0;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
Button refresh = (Button) findViewById(R.id.button);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
textLat = (TextView)findViewById(R.id.Lat);
textlong = (TextView)findViewById(R.id.Long);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
refresh.setEnabled(false);
refresh.setText("SEARCHING");
}
});
}
class myLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
textLat.setText(Double.toString(location.getLatitude()));
textlong.setText(Double.toString(location.getLongitude()));
lm.removeUpdates(ll);
refresh.setEnabled(true);
refresh.setText("SEARCH");
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
Button refresh = (Button) findViewById(R.id.button);
must be called after setContentView

Android GPS isn't providing updates anymore

My own android app isn't providing me with anymore GPS location updates. I've tried to build the simplest app possible and it's still not working. There's a blinking GPS icon in the status bar (so the GPS can't be turned off correct?), however I don't get locationChanged updates.
I have absolutely no clue what the problem is.
Manifest includes this:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Mainactivity.java
public class MainActivity extends ActionBarActivity {
private LocationManager locationManager;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
textView.setText(location.getLatitude() + " " + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
}
Change Activity to also implement LocationListener.
public class MainActivity extends ActionBarActivity implements LocationListener{
take the functions out of inner class, change the locationListener to this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0
, this); //note changed last parameter to this.
}
//The following methods were inside the inner class. They were not created in time
//for oncreate to requestLocationUpdates.
public void onLocationChanged(Location location) {
textView.setText(location.getLatitude() + " "
+ location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
Try that and see if it works better. The Activity itself is now a LocationListener, and the updates will come to this class.

How to return data in Android LocationListener

i want to build an app in which users can make foto which is tagged with the current location of the phone. So after making a foto, the user can save the picture by touching on the "save" button. If the button is touched the current location will be determined with my own LocationListener class. The Listener-class shows a Progressdialog and dismiss it, after a location is found. But now i want to know which i can return the location back to the calling Activity, because the Location listener methods are callback methods. Is there a "best-practice" solution for that, or have anybody a clue?
Location Listener:
public class MyLocationListener implements LocationListener {
private ProgressDialog progressDialog;
private Context mContext;
private LocationManager locationManager;
public MyLocationListener(Context context, ProgressDialog dialog) {
mContext = context;
progressDialog = dialog;
}
public void startTracking() {
locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, 10, 10, this);
progressDialog.show();
}
private void finishTracking(Location location) {
if(location != null) {
locationManager.removeUpdates(this);
progressDialog.hide();
Log.i("TRACKING",location.toString());
}
}
#Override
public void onLocationChanged(Location location) {
finishTracking(location);
}
#Override
public void onProviderDisabled(String provider) { }
#Override
public void onProviderEnabled(String provider) { }
#Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
Calling code:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Determine position...");
new MyLocationListener(this, dialog).startTracking();
Why not pass your own callback from activity to MyLocationListener and call its method from finishTracking?
Its a commonly used delegation pattern. Something like that:
class MyLocationListener implements LocationListener {
public interface MyListener {
void onLocationReceiver( Location location );
}
private MyListener listener;
public MyLocationListener(Context context, ProgressDialog dialog, MyListener listener) {
mContext = context;
progressDialog = dialog;
this.listener = listener;
}
private void finishTracking(Location location) {
if(location != null) {
locationManager.removeUpdates(this);
progressDialog.hide();
Log.i("TRACKING",location.toString());
listener.onLocationReceiver(location);
}
}
}
and call:
new MyLocationListener(this, dialog, new MyLocationListener.MyListener() {
public void onLocationReceived( Location location ) {
text.setText(location.toString());
}
}).startTracking();
You can do another simple thing - put your class MyLocationListener inside your Activity itself and modify the field in your activity inside your MyLocationListener itself.

Categories

Resources