如果启用了GPS,我的代码可以正常工作,但当需要启用时,它会强制崩溃。与未启用GPS时尝试获取位置有关。我试图为用户打开设置以启用GPS,但它只是继续尝试记录GPS(因此强制崩溃)。
我不知道何时何地给梅恩打电话。
第一个StartupSettings
被称为:
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);
}
}
上面的StartUpSettings() Activity
调用的enableGPS()
方法:
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;
}
然后我想调用Main
,它启动一个名为StartProcess()
的AsyncTask
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);
}
StartProcess AsyncTask
:
public class StartProcess extends AsyncTask<Main, Void, Void>
{
@Override
protected Void doInBackground(Main... params) {
LocationHandler2 lh = new LocationHandler2();
try {
lh.getLocationStartEnd(params[0],0);
}
catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
Logcat:
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)
小姐,我不知道你的问题是什么,但对于GPS,我做了这些教程http://thenewboston.org/list.php?cat=6它运行良好,不会崩溃。对于我不知道具体是哪一个的教程,它们应该是100多一点。你必须找到GPS的教程,这是30分钟的教程要做。我希望这是吗?
我最终将enableGPS()
方法移动到我的StartupSettings
活动,然后在用户启用GPS时使用startActivityForResult()
启动Main
。
不确定这是否理想,但已解决!