我有一个像这样配置的GPSTracker类:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
...
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
}
将立即调用此getLocation()
。现在我遇到的问题是在一个名为showSettingsAlert()
的函数中,在同一个类中实现,如下所示:
public void showSettingsAlert(){
new MaterialDialog.Builder(mContext)
.content("GPS Needs to be enabled to view Weather Data of your Location")
.title("Enable GPS")
.positiveText("ENABLE")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if (intent != null)
startActivity(intent);
else
Log.i("Null" , "Intent");
}
})
.negativeText("CANCEL")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
}
})
.show();
}
当显示此对话框时,当我单击ENABLE时,我得到以下转储:
04-15 16:59:12.288 3982-3982/com.a5corp.weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.a5corp.weather, PID: 3982
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.startActivity(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.startActivity(ContextWrapper.java:356)
at com.a5corp.weather.permissions.GPSTracker$2.onClick(GPSTracker.java:166)
at com.afollestad.materialdialogs.MaterialDialog.onClick(MaterialDialog.java:361)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6169)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
答案 0 :(得分:0)
private GPSTracker myGpsTracker;
protected void onCreate(Bundle savedInstanceState) {
myGpsTracker = new GPSTracker(this);
if (myGpsTracker.canGetLocation())
{
double longitude = myGpsTracker.latitude;
double latitude = myGpsTracker.longitude;
}
else
{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
showSettingsAlert();
}
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
*/
public void showSettingsAlert()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SplashScreen.this);
// Setting Dialog Title
alertDialog.setTitle("GPS Settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not active. Do you want to open?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, CommonUtils.REQUEST_CODE_ACTION_LOCATION_SOURCE_SETTINGS);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
setLocationValues();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CommonUtils.REQUEST_CODE_ACTION_LOCATION_SOURCE_SETTINGS) {
setLocationValues();
}
}
private void setLocationValues() {
myGpsTracker.getLocation();
if (myGpsTracker.canGetLocation()) {
double longitude = myGpsTracker.latitude;
double latitude = myGpsTracker.longitude;
}
}