Reputation: 406
My MainActivity call getLoc(MainActivity.this)
so it passes the context to my other activity called CurrentLocation. In CurrentLocation activity, getLoc(Context context1)
sets the global context which it is also used by onLocationChanged(Location location)
in order to call JsonTaskWeather()
but it seems that context is always null.
package com.tramtime.owner.croydontramtime;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.TextView;
import com.tramtime.owner.croydontramtime.Controllers.JsonTask.JsonTaskWeather;
import com.tramtime.owner.croydontramtime.Views.MainActivity;
public class CurrentLocation extends Activity implements LocationListener {
private LocationManager myManager;
private CurrentLocation loc;
private double latitude;
private double longitude;
private Context context;
@Override
public void onLocationChanged(Location location) {
String src = "lat="+location.getLatitude()+"&lon="+location.getLongitude();
JsonTaskWeather jsonTaskWeather = new JsonTaskWeather(context);
jsonTaskWeather.execute(
"http://api.openweathermap.org/data/2.5/weather?"+src+"&appid=e58a3347bfbb6123ea384743a3f2e686");
}
public String see(){
String foo = "lat and lon"+latitude+" "+longitude;
return foo;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@TargetApi(Build.VERSION_CODES.M)
public void getLoc(Context context1) {
this.context = context1;
loc = new CurrentLocation();
myManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
}, 10);
return;
}else{
myManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, loc, Looper.myLooper());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED);
}
}
}
This is what it logs out:
08-28 08:26:11.607 8752-8752/com.tramtime.owner.croydontramtime E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tramtime.owner.croydontramtime, PID: 8752
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at com.tramtime.owner.croydontramtime.CurrentLocation.onLocationChanged(CurrentLocation.java:36)
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:288)
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:217)
at android.location.LocationManager$ListenerTransport$2.handleMessage(LocationManager.java:240)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5507)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-28 08:26:11.704 8752-8752/com.tramtime.owner.croydontramtime I/Process: Sending signal. PID: 8752 SIG: 9
08-28 08:33:11.592 9367-9367/com.tramtime.owner.croydontramtime I/logging again: 0.0
08-28 08:33:11.624 9367-9447/com.tramtime.owner.croydontramtime I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
Upvotes: 0
Views: 3257
Reputation: 2297
The issue is this is a flawed design. Firstly, I am not able to understand how you are able to call the getLoc function if its not static ? Is MainActivity extending the CurrentLocation class ?
Anyways here is a suggestion on how to make it work
1) Directly implement the LocationListener in MainActivity
2) Use intent to call the CurrentLocation class and inside the onCreate of CurrentLocation update the content by doing the following
Content context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
....
}
Now the context is properly initialized and you can use it anywhere in the class.
Upvotes: 1