当我尝试运行我的代码时,我总是收到此错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.content.res.Resources android.content.Context.getResources()'
我可以在我的模拟器中手动配置坐标,但仍然会出现同样的错误。
以下是获取位置坐标的代码。
LocationTracker.java
public class LocationTracker extends MainActivity implements LocationListener {
MainActivity activity;
Context context;
Location location;
LocationManager lm;
double lat,longi;
boolean isGpsEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetNetworkLocation = false;
public LocationTracker (Context context) {
this.context=context;
getGPSLocation();
}
public Location getGPSLocation() {
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGpsEnabled) {
/** Check for RUNTIME PERMISSION */
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (location == null) { //Request for NEW DATA coming from GPS
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
if (lm != null) { //Retrieve data from the earlier
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
lat= location.getLatitude();
longi= location.getLongitude();
}
}
}
}else {
ActivityCompat.requestPermissions(activity,new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},123);
}
}else {
}
return location;
}
/** GET LATITUDE METHOD */
public double getLatitude() {
if (location != null) {
lat=location.getLatitude();
}
return lat;
}
/** GET LONGITUDE METHOD */
public double getLongitude () {
if (location != null) {
longi=location.getLongitude();
}
return longi;
}
@Override
public void onLocationChanged(Location location) {
lat= location.getLatitude();
longi=location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
用于显示坐标的和 MainActivity类。
public class MainActivity extends AppCompatActivity {
Context context;
DBHelper helper;
LocationTracker tracker;
TextView contactsTv,sendEmergencyTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new DBHelper(getApplicationContext());
/**
* OnClick Listener in android
*/
contactsTv=(TextView) findViewById(R.id.tv_createContacts);
contactsTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(MainActivity.this, ContactsActivity.class); //redirect to the COntacts dsadsadsa
startActivity(intent);
}
});
sendEmergencyTv=(TextView)findViewById(R.id.tv_sendEmergency);
sendEmergencyTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tracker= new LocationTracker(MainActivity.this);
double lat,longi;
lat= tracker.getLatitude();
longi= tracker.getLongitude();
Toast.makeText(context,"Latitude: "+lat+ "Longitude: "+longi,Toast.LENGTH_LONG).show();
}
});
}
}
顺便说一下,我不确定在RequestPermission参数中的LocationTracker类中放置一个MainActivity实例。
答案 0 :(得分:0)
下面:
ActivityCompat.requestPermissions(activity,
new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},123);
由于activity
为null
,行导致问题。
使用((Activity)context)
代替activity
。例如:
ActivityCompat.requestPermissions(((Activity)context),
new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},123);
注意:强>
另外,因为LocationTracker
是普通的java类,所以不需要在其中扩展Activity
。
答案 1 :(得分:0)
在第
行ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},123);
activity
为空,因为它从未被赋值。
将LocationTracker
构造函数更改为Activity
而不是Context
作为参数。
private Activity activity;
public LocationTracker (Activity activity) {
this.activity = activity;
getGPSLocation();
}
注意:请务必在该活动中实施onRequestPermissionsResult
,以了解您是否已获得授权。