Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to structure code when need to enable GPS
    primarykey
    data
    text
    <p>My code works fine if the GPS is enabled, but when it needs to be enabled it force crashes. Something to do with trying to get the location when the GPS is not enabled. I try to open up Settings for the user to enable GPS but it just continues trying to record GPS (hence force-crashing).</p> <p>I don't know when and where to call Main.</p> <p>First <code>StartupSettings</code> is called:</p> <pre><code>public class StartupSettings extends Activity{ boolean hasGps = false; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.startup_dialog); checkGPS(); } public void checkGPS() { LocationHandler2 lh = new LocationHandler2(); hasGps = lh.enableGPS(this); if (hasGps == true) { TextView tv = (TextView) findViewById(R.id.checkGPSConnection); tv.setTextColor(Color.GREEN); } } </code></pre> <p>The <code>enableGPS()</code> method called by the <code>StartUpSettings() Activity</code> above:</p> <pre><code>public class LocationHandler2{ LocationManager mlocManager; public boolean enableGPS(final StartupSettings main) { mlocManager = (LocationManager)main.getSystemService(Context.LOCATION_SERVICE); if(!mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { //Ask the user to enable GPS AlertDialog.Builder builder = new AlertDialog.Builder(main); builder.setTitle("Location Manager"); builder.setMessage("Would you like to enable GPS?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Launch settings, allowing user to make a change Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); main.startActivity(i); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //No location service, no Activity main.finish(); } }); builder.create().show(); } else { return true; } return true; } </code></pre> <p>I want to then call <code>Main</code> which starts up an <code>AsyncTask</code> called <code>StartProcess()</code></p> <pre><code>public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pages); StartProcess sProcess = new StartProcess(); sProcess.execute(this); } </code></pre> <p>The <code>StartProcess AsyncTask</code>:</p> <pre><code>public class StartProcess extends AsyncTask&lt;Main, Void, Void&gt; { @Override protected Void doInBackground(Main... params) { LocationHandler2 lh = new LocationHandler2(); try { lh.getLocationStartEnd(params[0],0); } catch (InterruptedException e) { e.printStackTrace(); } return null; } } </code></pre> <p>Logcat:</p> <pre><code>02-26 17:33:31.264: E/AndroidRuntime(10560): FATAL EXCEPTION: AsyncTask #1 02-26 17:33:31.264: E/AndroidRuntime(10560): java.lang.RuntimeException: An error occured while executing doInBackground() 02-26 17:33:31.264: E/AndroidRuntime(10560): at android.os.AsyncTask$3.done(AsyncTask.java:200) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.lang.Thread.run(Thread.java:1096) 02-26 17:33:31.264: E/AndroidRuntime(10560): Caused by: java.lang.IllegalArgumentException: provider==null 02-26 17:33:31.264: E/AndroidRuntime(10560): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:653) 02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.LocationHandler2.getLocationStartEnd(LocationHandler2.java:120) 02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.StartProcess.doInBackground(StartProcess.java:13) 02-26 17:33:31.264: E/AndroidRuntime(10560): at mfc.generalguixapi8.StartProcess.doInBackground(StartProcess.java:1) 02-26 17:33:31.264: E/AndroidRuntime(10560): at android.os.AsyncTask$2.call(AsyncTask.java:185) 02-26 17:33:31.264: E/AndroidRuntime(10560): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 02-26 17:33:31.264: E/AndroidRuntime(10560): ... 4 more 02-26 17:34:37.694: E/global(10620): Deprecated Thread methods are not supported. 02-26 17:34:37.694: E/global(10620): java.lang.UnsupportedOperationException 02-26 17:34:37.694: E/global(10620): at java.lang.VMThread.stop(VMThread.java:85) 02-26 17:34:37.694: E/global(10620): at java.lang.Thread.stop(Thread.java:1379) 02-26 17:34:37.694: E/global(10620): at java.lang.Thread.stop(Thread.java:1344) 02-26 17:34:37.694: E/global(10620): at mfc.generalguixapi8.SplashScreen$1.run(SplashScreen.java:36) 02-26 17:36:18.134: E/global(10713): Deprecated Thread methods are not supported. 02-26 17:36:18.134: E/global(10713): java.lang.UnsupportedOperationException 02-26 17:36:18.134: E/global(10713): at java.lang.VMThread.stop(VMThread.java:85) 02-26 17:36:18.134: E/global(10713): at java.lang.Thread.stop(Thread.java:1379) 02-26 17:36:18.134: E/global(10713): at java.lang.Thread.stop(Thread.java:1344) 02-26 17:36:18.134: E/global(10713): at mfc.generalguixapi8.SplashScreen$1.run(SplashScreen.java:36) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload