当我试图从GPS提供商那里获得坐标时,我收到了这个错误。
package com.example.task1;
public class GPSTracker extends Service implements LocationListener
{
private Context Mycontext;
boolean IsGPSEnabled = false;
boolean IsNetworkEnabled = false;
boolean CanGetLocation = false;
Location location;
double Latitude;
double Longitude;
private static final int MinDstance = 0; // minimum distance to change
// update in meters
private static final long MinTime = 0;// minimum time between update in
// milisec
protected LocationManager LocationMgr;
public void GPSTracker1(Context context, TextView vieww)
{
this.Mycontext = context;
GetLocation(vieww);
}
public Location GetLocation(TextView view)
{
try {
LocationMgr = (LocationManager) Mycontext
.getSystemService(LOCATION_SERVICE);
IsGPSEnabled = LocationMgr
.isProviderEnabled(LocationManager.GPS_PROVIDER);
IsNetworkEnabled = LocationMgr
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!IsGPSEnabled && !IsNetworkEnabled) {
view.setText("No network availables");
} else {
this.CanGetLocation = true;
if (IsNetworkEnabled) {
LocationMgr.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, MinDstance,
MinTime, this);
Log.d("Network", "Network");
if (LocationMgr != null) {
location = LocationMgr
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
updateGPSCoordinator();
}
}
if (IsGPSEnabled) {
if (location == null) {
LocationMgr.requestLocationUpdates(
LocationManager.GPS_PROVIDER, MinDstance,
MinTime, this);
Log.d("GPS enabled", "GPS Enalbed");
if (LocationMgr != null) {
location = LocationMgr
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateGPSCoordinator();
}
}
}
}
/*
* if(location != null) { Latitude = location.getLatitude();
* Longitude = location.getLongitude();
*
*
* }
*/
} catch (StackOverflowError e) {
Log.e("Error : Location",
"Impossible to connect to LocationManager", e);
}
return location;
}
public void updateGPSCoordinator()
{
if (location != null) {
Latitude = location.getLatitude();
Longitude = location.getLongitude();
}
}
public void stopUsingGPS()
{
if (LocationMgr != null) {
LocationMgr.removeUpdates(GPSTracker.this);
}
}
public double getLatitude()
{
if (location != null) {
Latitude = location.getLatitude();
}
return Latitude;
}
public double getLongitude() {
if (location != null) {
Longitude = location.getLongitude();
}
return Longitude;
}
boolean canGetlocation() {
return this.canGetlocation();
}
public void showSettingsAlert() {
AlertDialog.Builder AlertDialog = new AlertDialog.Builder(Mycontext);
AlertDialog.setTitle("GPS IS OFF");
AlertDialog
.setMessage("GPS Is Not enabled Do you want to go to settings");
AlertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
Mycontext.startActivity(intent);
}
});
AlertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog.show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
这是主要活动代码:
package com.example.task1;
public class Main_Page extends Activity {
Button Run_btn;
TextView View_txt;
GPSTracker gps;
ProgressBar pb;
List<MyTasks> tasks;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
View_txt = (TextView) findViewById(R.id.Long_lat_txt);
Run_btn = (Button) findViewById(R.id.Run_btn);
tasks = new ArrayList();
Run_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gps = new GPSTracker();
gps.GPSTracker1(Main_Page.this, View_txt);
if (IsOnline()) {
RequestData();
} else {
View_txt.setText("No network available");
}
if (gps.canGetlocation()) {
double latitude = gps.getLatitude();
double longtude = gps.getLongitude();
View_txt.setText("current location Is" + latitude + " "
+ longtude);
} else {
gps.showSettingsAlert();
}
}
});
}
protected void updatedisplay(String message) {
View_txt.append(message + "\n");
}
private void RequestData() {
MyTasks Task = new MyTasks();
Task.execute("P 1 ", "P 2 ", "P 3 ");
}
protected boolean IsOnline() {
ConnectivityManager ConMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NetInfo = ConMgr.getActiveNetworkInfo();
if (NetInfo != null && NetInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
private class MyTasks extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
for (int i = 0; i < params.length; i++) {
publishProgress("Work With" + params[i]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Task complete";
}
@Override
protected void onProgressUpdate(String... values) {
updatedisplay(values[0]);
}
}
}
这是日志错误:
12-07 11:18:37.581: E/AndroidRuntime(517): FATAL EXCEPTION: main
12-07 11:18:37.581: E/AndroidRuntime(517): java.lang.StackOverflowError
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
12-07 11:18:37.581: E/AndroidRuntime(517): at com.example.task1.GPSTracker.canGetlocation(GPSTracker.java:179)
答案 0 :(得分:3)
你在这里有无限的递归:
boolean canGetlocation()
{
return this.canGetlocation();
}
将其更改为:
boolean canGetlocation()
{
return this.CanGetLocation;
}
顺便说一下,你应该调用你的实例变量和方法canGetLocation
来尊重Java的命名约定。